JSONArray.fromObject(list.toArray());
list集合对象中如果有Date类型的字段,数据一定要是TIMESTAMP不能是DATE,就会报以下错误:
net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
出现死循环报异常:
threw an exception when invoked on net.sf.json.JSONException: There is a cycle in the hierarchy!"
用如下方法过滤字段:
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
jsonConfig.setExcludes(new String[]{"roles"});//过滤字段
JSONArray jsonArray = JSONArray.fromObject(list,jsonConfig);