执行:
JSONArray array = JSONArray.fromObject(this.users);
就会报以下错误:
net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
users是一个list集合
方案一:
JSONArray array = JSONArray.fromObject(this.users.toArray());
方案二:
因为bean里有Date字段,且从
代码
JsonConfig cfg = new JsonConfig(); cfg.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
方法举例
Java代码
/** * datagrid easyui 查找出联系人pager-公共的,和自已创建的可以查看 */ @Transactional(readOnly = true) public JSONArray datagrid(Pager page,User user,DetachedCriteria detachedCriteria){ //有级联,不能直接转化,要取出List放到map里面 JsonConfig cfg = new JsonConfig(); //过滤关联,避免死循环net.sf.json.JSONException: java.lang.reflect.InvocationTargetException cfg.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object source, String name, Object value) { if(name.equals("addressGroup")||name.equals("user")||name.equals("createTime")||name.equals("birthday")) { return true; } else { return false; } } }); //net.sf.json.JSONException: java.lang.reflect.InvocationTargetException异常 cfg.setExcludes(new String[]{"handler","hibernateLazyInitializer"}); //javabean里出现循环调用啦,赶快用excludes干掉parent或者children // cfg.setExcludes(new String[]{"addressGroup"}); //net.sf.json.JSONException: java.lang.reflect.InvocationTargetException日期格式转化出错 // cfg.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); //cfg.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd hh:mm:ss")); Pager pager=this.findAllByApprove(page, user, detachedCriteria); long total=pager.getTotalCount(); List list=pager.getResult(); Map result=new HashMap(); result.put("total", total); result.put("rows", list); JSONArray jsonArray = JSONArray.fromObject(result,cfg); return jsonArray; }