转: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. [翻译] flask-SocketIO

    最近开发工作需要用到websocket去替代老办法轮询,因为我们的web系统使用flask搭建,所以使用flask-SocketIO作为我们的websocket方案,因此顺手翻译官方文档 *** Fl ...

  2. 【python】-- Django 分页 、cookie、Session、CSRF

    Django  分页 .cookie.Session.CSRF 一.分页 分页功能在每个网站都是必要的,下面主要介绍两种分页方式: 1.Django内置分页 from django.shortcuts ...

  3. 作为一名合格的JAVA程序员需要点亮那些技能树?

    以下是出现次数超过100的一些技能,大家可以做一个参考. Spring 299 MySQL 290 JavaScript 216Linux 165J2EE 151设计模式 148Struts2 138 ...

  4. Selenium获取input输入框中值的几种方法

    1.selenium 自带的方法 get_attribute('value') driver.find_element_by_name("f[url]").get_attribut ...

  5. 2.6 基于ARDUINO UNO+MC20的路径显示功能

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...

  6. css的继承性理解

    1) 所有的text 相关属性都被继承: 如 font-family font-size; font-style;font-weight;font;font-variant;letter-spacin ...

  7. Swift中字典解析后的问题,!?两种拆包的差别

    给出一个json,使用SwiftyJSON解析传给model,传进去是个字典,字典里有String,NSNumber,NSDoctionary,和NSArray. 正常情况下直接使用下面的解析方法即可 ...

  8. 生成sql表结构

    DataConstruct.php <?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/7/21 * Time ...

  9. scope的继承

    本文转载自: http://www.tuicool.com/articles/63iEref angular中scope的继承与js的原型继承有密切的联系,首先回顾一下js的继承: function ...

  10. Kubernetes 部署Mysql 主从复制集群

    Mysql主从参考文章: https://www.jianshu.com/p/509b65e9a4f5 http://blog.51cto.com/ylw6006/2071864 Statefulse ...