转:json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

执行:
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字段,且从数据库里读出来的是java.sql.Date赋值给了java.util.Date,转化成JSONArray时出错;可以在从数据库读出Date 时直接写成:new java.util.Date(rs.getDate("date").getTime),这样就不会出错了;

方案三:

  1. 日期格式
  2. hibernate延时加载

1.解决:日期格式

 
private java.util.Date createTime; 

只在字段前声明Date的数据类型可能也会抛异常,在Set,get方法中,有出现Date类型的都把包名加上

2.解决:hibernate延时加载 设置

  1. JsonConfig cfg = new JsonConfig();
  2. cfg.setExcludes(new String[]{"handler","hibernateLazyInitializer"});

方法举例

    1. /**
    2. * datagrid easyui 查找出联系人pager-公共的,和自已创建的可以查看
    3. */
    4. @Transactional(readOnly = true)
    5. public JSONArray datagrid(Pager<AddressBook> page,User user,DetachedCriteria detachedCriteria){
    6. //有级联,不能直接转化,要取出List放到map里面
    7. JsonConfig cfg = new JsonConfig();
    8. //过滤关联,避免死循环net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
    9. cfg.setJsonPropertyFilter(new PropertyFilter()
    10. {
    11. public boolean apply(Object source, String name, Object value) {
    12. if(name.equals("addressGroup")||name.equals("user")||name.equals("createTime")||name.equals("birthday")) {
    13. return true;
    14. } else {
    15. return false;
    16. }
    17. }
    18. });
    19. //net.sf.json.JSONException: java.lang.reflect.InvocationTargetException异常
    20. cfg.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
    21. //javabean里出现循环调用啦,赶快用excludes干掉parent或者children
    22. // cfg.setExcludes(new String[]{"addressGroup"});
    23. //net.sf.json.JSONException: java.lang.reflect.InvocationTargetException日期格式转化出错
    24. // cfg.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
    25. //cfg.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd hh:mm:ss"));
    26. Pager<AddressBook> pager=this.findAllByApprove(page, user, detachedCriteria);
    27. long total=pager.getTotalCount();
    28. List<AddressBook> list=pager.getResult();
    29. Map<String,Object> result=new HashMap<String,Object>();
    30. result.put("total", total);
    31. result.put("rows", list);
    32. JSONArray jsonArray  = JSONArray.fromObject(result,cfg);
    33. return jsonArray;
    34. }

json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException的更多相关文章

  1. json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

    注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...

  2. java.sql.Date赋值给了java.util.Date.转化成JSONArray时出错net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

    net.sf.json.JSONException: java.lang.reflect.InvocationTargetExceptionat net.sf.json.JSONObject.defa ...

  3. net.sf.json.JSONException: java.lang.reflect.InvocationTargetException Caused by: java.lang.IllegalArgumentException at java.sql.Date.getHours(Unknown Source)

    数据库字段类型为Date,转成JSON格式会有问题,解决方案如下: json-lib有一个配置类JsonConfig通过JsonConfig可以注册一个字段处理器实现JsonValueProcesso ...

  4. root cause:org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException

    今天在调试SSH与Ajax时,服务器端报出JSON异常:

  5. 本地tomcat调用远程接口报错:java.lang.reflect.InvocationTargetException

    今天碰到一个奇怪的问题,本地Eclipse起了一个tomcat通过http去调一个外部接口,结果竟然报了一个反射的异常,先看下完整日志: , :: 下午 org.apache.catalina.sta ...

  6. SpringBoot项目启动报错:java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

    .   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | ...

  7. 在 Linux 环境下报错 java.lang.reflect.InvocationTargetException

    今天开发了一个 excel 导出数据的功能,放到 linux 服务器上后发现报错. 捕获到 java.lang.reflect.InvocationTargetException 异常,这个异常不太常 ...

  8. SQLException: com.mchange.v2.c3p0.ComboPooledDataSource [ java.beans.IntrospectionException: java.lang.reflect.InvocationTargetException [numThreadsAwaitingCheckoutDefaultUser] ] has been closed()

    问题:Could not get JDBC Connection; nested exception is java.sql.SQLException: com.mchange.v2.c3p0.Com ...

  9. 错误: java.lang.reflect.InvocationTargetException

    错误: java.lang.reflect.InvocationTargetException    at sun.reflect.NativeMethodAccessorImpl.invoke0(N ...

随机推荐

  1. WCF基础之事务

    说到事务,我最先想到的是“回滚”. 百科:事务是恢复和并发控制的基本单位.事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.好了,具体的就不多复制了. 我小试了一下 ...

  2. Springboot中读取自定义名称properties的

    Springboot读取自定义的配置文件时候,使用@value,一定要指定配置文件的位置!  否则报错参数化异常!

  3. vs编译程序不能实现,“未能完成操作 未指定的错误”的解决办法

    1.先把.vcproj 文件剪切到其他地方 2.打开.sln,报错->点“确定”->再点“确定” 3.把 .vcproj 文件 放回来,在vs2005右边的“解决方案”处右键 重新加载,就 ...

  4. 常用的JS代码块收集

    /**数组去重一*/ (function (arr) { arr = arr.sort(); for (var i = 0; arr[i]; i++) { if (arr[i] === arr[i + ...

  5. Armijo-Goldstein准则与Wolfe-Powell准则

    Armijo-Goldstein准则与Wolfe-Powell准则是不精确的一维搜索的两大准则. 之所以要遵循这些准则是为了能使算法收敛(求最优解).即要使我们的不精确的一维搜索的步长满足一定的规则, ...

  6. 3.6中的range()

    在python3中range()是这样的: >>> range(4) range(0, 4) #额,列表跑哪去了 在之前的python2中是这样的: >>> ran ...

  7. select + range + insertNode+offset

    self. cmd.range.selectNodeContents(p[0])   建立选区 self.cmd.select()   选中选区 self.cmd.selection([forceRe ...

  8. 函数没有返回值,默认返回undefined

    var  a =( function(){return})(); a = undefined;

  9. Loadrunder之脚本篇——参数化取值策略

    参数取值选项 Select next row Update value on 以上两个选项是改变参数化取值的关键选项. Select next row包含如下选项: Sequential:顺序选择 R ...

  10. 【HackerRank】Cut the tree

    题目链接:Cut the tree 题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小. 暴力求解会超时. 如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a ...