转: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. 阻塞队列 ArrayBlockingQueue 我给自己挖了一个坑

    说一句MMB, 一下午时间, package cn.tbnb1.seckil.quene; import java.util.concurrent.ArrayBlockingQueue; import ...

  2. Adjacency List

    w Python Patterns - Implementing Graphs | Python.orghttps://www.python.org/doc/essays/graphs/ Graph ...

  3. 如何生成动态库 .dll 的符号 .lib 文件?

    在知道拥有动态库和头文件的情况下,但没有动态库符号文件的情况下,如何静态链接动态库? 1.使用 Microsoft Visual Studio Tools 命令行命令生成 xxx.def 文件,进而使 ...

  4. python下多线程的限制以及多进程中传递参数的方式

    python多线程有个全局解释器锁(global interpreter lock),这个锁的意思是任一时间只能有一个线程使用解释器,跟单cpu跑多个程序一个意思,大家都是轮着用的,这叫“并发”,不是 ...

  5. mprotect() failed: Cannot allocate memory

    遇到这个问题是在測试项目的性能时发现的,每一个对象分配一页大小的空间然后mprotect() 保护起来,当系统分配3W多个页的时候会出现这个问题. google到在一份邮件列表中也曾提到该问题.htt ...

  6. Windows系统Python 安装第三方模块时,提示pip版本有问题

    如果按照提示输入python -m pip install --upgrade pip 还不行, 那么执行easy_install --upgrade pip 即可 参考:https://stacko ...

  7. 20170411 F-02创建财务凭证

    SAPMF05A 100 X       0   BDC_OKCODE /00   0   BKPF-BLDAT 20170411   0   BKPF-BLART SA   0   BKPF-BUK ...

  8. PowerDesigner数据模型(CDM—PDM)

    操作过程 点击:  Tools/Generate Physical Data Model

  9. hadoop学习第二天-了解HDFS的基本概念&&分布式集群的搭建&&HDFS基本命令的使用

    一.HDFS的相关基本概念 1.数据块 1.在HDFS中,文件诶切分成固定大小的数据块,默认大小为64MB(hadoop2.x以后是128M),也可以自己配置. 2.为何数据块如此大,因为数据传输时间 ...

  10. 由于Redis漏洞导致服务器被黑

    原文地址 漏洞描述 Redis默认情况下,会绑定在0.0.0.0:6379,这样将会将Redis服务暴露到公网上,如果在没有开启认证的情况下,可以导致任意用户在可以访问目标服务器的情况下未授权访问Re ...