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 ...
随机推荐
- MySQL 的约束
约束是添加在列上, 用来约束列的! 1. 主键约束(表中的某行的唯一标识) 主键的特点: 非空 唯一 被引用 创建表时, 指定主键的两种方式: // 需求: 指定 sid 列为主键列, 即为 sid ...
- vertical-align:middle;一般用于img和行内文字对齐方式
vertical-align:top ;文字和行内块元素的顶部对齐 vertical-align:middle;居中 vertical-align:bottom;底对齐
- MySQL中事务的分类
从事务理论的角度来看,可以把事务分为以下几种类型 扁平事务(Flat Transactions) 带有保存点的扁平事务(Flat Transactions with Savepoints) 链事务(C ...
- tfboys——tensorflow模块学习(二)
tf.contrib模块 tf.contrib 模块是一个比较复杂的模块. contrib细节: tf.contrib.bayesflow.entropy 香农信息论 tf.contrib.baye ...
- Django为什么要跳转到不同的页面来实现不同的功能
其实是不同将信息提交给不同的页面交给不同的页面去处理同一个数据库,不同的模块实现不同的功能,当要实现某一个功能的时候直接跳转到那一个功能下面的url,可以把要实现的功能区分开,以python面向对象的 ...
- 模块调用,datetime,time,logging,递归,双层装饰器, json,pickle迭代器和生成器
一.python模块(导入,内置,自定义,开源) 1.模块简介 模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py.模块可以被别的程序引入,以使用该模块中的函数等功能.这也是使用python ...
- Yii2.0数据库查询实例(三)
常用查询: // WHERE admin_id >= 10 LIMIT 0,10 User::find()->])->offset()->limit()->all() / ...
- CKEditor & CKFinder集成
CKEditor集成 CKEditor(原名FckEditor): 著名的HTML编辑器(可在线编辑HTML) 配置: ①将CKEditor中的(adapters images lang plugin ...
- C语言中auto,register,static,const,volatile的区别
1)auto 这个关键字用于声明变量的生存期为自动,即将不在任何类.结构.枚举.联合和函数中定义的变量视为全局变量,而在函数中定义的变量视为局部变量.这个关键字不怎么多写,因为所有的变量默认就是aut ...
- 【leetcode刷题笔记】Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters ...