json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
转: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),这样就不会出错了;
方案三:
- 日期格式
- hibernate延时加载
1.解决:日期格式
只在字段前声明Date的数据类型可能也会抛异常,在Set,get方法中,有出现Date类型的都把包名加上
2.解决:hibernate延时加载 设置
- JsonConfig cfg = new JsonConfig();
- cfg.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
方法举例
- /**
- * datagrid easyui 查找出联系人pager-公共的,和自已创建的可以查看
- */
- @Transactional(readOnly = true)
- public JSONArray datagrid(Pager<AddressBook> 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<AddressBook> pager=this.findAllByApprove(page, user, detachedCriteria);
- long total=pager.getTotalCount();
- List<AddressBook> list=pager.getResult();
- Map<String,Object> result=new HashMap<String,Object>();
- result.put("total", total);
- result.put("rows", list);
- JSONArray jsonArray = JSONArray.fromObject(result,cfg);
- return jsonArray;
- }
json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException的更多相关文章
- json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...
- 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 ...
- 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 ...
- root cause:org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
今天在调试SSH与Ajax时,服务器端报出JSON异常:
- 本地tomcat调用远程接口报错:java.lang.reflect.InvocationTargetException
今天碰到一个奇怪的问题,本地Eclipse起了一个tomcat通过http去调一个外部接口,结果竟然报了一个反射的异常,先看下完整日志: , :: 下午 org.apache.catalina.sta ...
- SpringBoot项目启动报错:java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | ...
- 在 Linux 环境下报错 java.lang.reflect.InvocationTargetException
今天开发了一个 excel 导出数据的功能,放到 linux 服务器上后发现报错. 捕获到 java.lang.reflect.InvocationTargetException 异常,这个异常不太常 ...
- 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 ...
- 错误: java.lang.reflect.InvocationTargetException
错误: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(N ...
随机推荐
- 阻塞队列 ArrayBlockingQueue 我给自己挖了一个坑
说一句MMB, 一下午时间, package cn.tbnb1.seckil.quene; import java.util.concurrent.ArrayBlockingQueue; import ...
- Adjacency List
w Python Patterns - Implementing Graphs | Python.orghttps://www.python.org/doc/essays/graphs/ Graph ...
- 如何生成动态库 .dll 的符号 .lib 文件?
在知道拥有动态库和头文件的情况下,但没有动态库符号文件的情况下,如何静态链接动态库? 1.使用 Microsoft Visual Studio Tools 命令行命令生成 xxx.def 文件,进而使 ...
- python下多线程的限制以及多进程中传递参数的方式
python多线程有个全局解释器锁(global interpreter lock),这个锁的意思是任一时间只能有一个线程使用解释器,跟单cpu跑多个程序一个意思,大家都是轮着用的,这叫“并发”,不是 ...
- mprotect() failed: Cannot allocate memory
遇到这个问题是在測试项目的性能时发现的,每一个对象分配一页大小的空间然后mprotect() 保护起来,当系统分配3W多个页的时候会出现这个问题. google到在一份邮件列表中也曾提到该问题.htt ...
- Windows系统Python 安装第三方模块时,提示pip版本有问题
如果按照提示输入python -m pip install --upgrade pip 还不行, 那么执行easy_install --upgrade pip 即可 参考:https://stacko ...
- 20170411 F-02创建财务凭证
SAPMF05A 100 X 0 BDC_OKCODE /00 0 BKPF-BLDAT 20170411 0 BKPF-BLART SA 0 BKPF-BUK ...
- PowerDesigner数据模型(CDM—PDM)
操作过程 点击: Tools/Generate Physical Data Model
- hadoop学习第二天-了解HDFS的基本概念&&分布式集群的搭建&&HDFS基本命令的使用
一.HDFS的相关基本概念 1.数据块 1.在HDFS中,文件诶切分成固定大小的数据块,默认大小为64MB(hadoop2.x以后是128M),也可以自己配置. 2.为何数据块如此大,因为数据传输时间 ...
- 由于Redis漏洞导致服务器被黑
原文地址 漏洞描述 Redis默认情况下,会绑定在0.0.0.0:6379,这样将会将Redis服务暴露到公网上,如果在没有开启认证的情况下,可以导致任意用户在可以访问目标服务器的情况下未授权访问Re ...