转: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. 巨蟒django之CRM4 一些小功能

    内容回顾: 修改的地方 (1) (2) (3) (4) (5) 整体回顾前几天内容: 现在可以登录的原因,session内部存储了信息 这个时候我们再访问刚才的地址,会发现,跳转到了登录页面login ...

  2. python并发编程&IO模型

    一 IO模型介绍 为了更好地了解IO模型,可先回顾下:同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(n ...

  3. Python3.6全栈开发实例[003]

    3.检查传入列表的长度,如果大于2,将列表的前两项内容返回给调用者. li = [11,22,33,44,55,66,77,88,99,000,111,222] def func3(lst): if ...

  4. Linux中的环境变量配置文件及其作用

    登录相关的配置文件: /etc/profile 范围:对所有用户生效 作用: a.定义USER变量 b.定义LOGNAME变量 c.定义MAIL变量 d.定义PATH变量 e.定义HOSTNAME变量 ...

  5. HDFS权限

    1.1 超级用户 启动namenode服务的用户就是超级用户, 该用户的组是supergroup 1.2 文件权限管理   1.2.1 创建时的owner和group 文件或者目录被创建之时,服从BS ...

  6. win7与win server 2008防火墙设置

    转自:http://blog.51cto.com/jimshu/590411 Windows 防火墙通过阻止未授权用户通过 Internet 或网络访问您的计算机来帮助保护计算机. Windows 2 ...

  7. create ‘/.git/index.lock’: File exists.

    Git – fatal: Unable to create ‘/.git/index.lock’: File exists. fatal: Unable to create ‘/path/my_pro ...

  8. If you ever have a broken heart

    If you ever have a broken heart Pablo Neruda Tonight i can write the saddest lines Write ,for exampl ...

  9. python爬取百度翻译返回:{'error': 997, 'from': 'zh', 'to': 'en', 'query 问题

    解决办法: 修改url为手机版的地址:http://fanyi.baidu.com/basetrans User-Agent也用手机版的 测试代码: # -*- coding: utf-8 -*- & ...

  10. PyQt4打包exe文件

    使用到工具pyinstaller pip install pyinstaller 使用指令打包,其中xxx.ico为需要的图标,xxx.py为要打包的py文件 pyinstaller -w --ico ...