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 ...
随机推荐
- react-native 中使用redux 优化 Connect 使用装饰器简化代码报错
报错信息 error: bundling failed: Error: The 'decorators' plugin requires a 'decoratorsBeforeExport' opti ...
- Delphi Pdf的使用方法
此方法安装了llPDFLib.v3.6 控件.对pdf左侧.右侧正文进行了操作. procedure TForm1.Button1Click(Sender: TObject); var node,nd ...
- DBProxy 入门到精通系列(一):DBProxy概述
DBProxy概述 DBProxy是支持分库分表,读写分离的中间件,博客内容除了阅读github官方文档外,还会附加上本人个人测试的结果,但了解一个中间件,需要对这个中间件有一个基本的认识,方便控制. ...
- python并发编程&IO模型
一 IO模型介绍 为了更好地了解IO模型,可先回顾下:同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(n ...
- Java输入输出重定向代码
try { BufferedInputStream in = new BufferedInputStream(new FileInputStream("input.txt")) ...
- MySql存储过程、函数
存储过程和函数是在数据库中定义一些SQL语句的集合,然后直接调用这些存储过程和函数来执行已经定义好的SQL语句.存储过程和函数可以避免开发人员重复的编写相同的SQL语句.而且,存储过程和函数是在MyS ...
- 江卓尔与比特币增发,谣言or先知-千氪
最近,很多圈内人都在讨论比特币是否应该增发,但等等,比特币真的会增发吗?到底是真有增发计划还是某些人别有用心地在散布谣言? 那么消息是从哪里出来的?北京时间 2 月 10 日晚,莱比特矿池创始人江卓尔 ...
- 4.3 使用STM32控制MC20进行GPRS通讯
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- js对象属性方法大总结(收集)
数组(Array):系列元素的有序集合: 详细演示请看:[js入门系列演示·数组 ] http://www.cnblogs.com/thcjp/archive/2006/08/04/467761.ht ...
- 微服务架构~BFF和网关是如何演化出来的
介绍 BFF(Backend for Frontend)和网关Gateway是微服务架构中的两个重要概念,这两个概念相对比较新,有些开发人员甚至是架构师都不甚理解. 本文用假想的公司案例+图示的方式, ...