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 ...
随机推荐
- 使用国内源(brew, pip, npm)
如果网络不行,安装依赖包的速度小于 100k/s 或者丢包严重导致安装很慢,我认为就应该使用国内源了.今天因为国内源的问题弄了很久,我觉得在国内服务器部署,全部应该从国内下载资源.分为两种情况, 存在 ...
- Pythonpika PhpAmqpLib rabbitmq服务中queues被清空的异常处理 无模式数据库对数据结构的定义和控制
/** * Declares queue, creates if needed * * @param string $queue * @param bool $passive * @param boo ...
- PHP7-MySQLi在分页中的应用
获取页码,设置每页行数 $page = $_POST["page"]; if($page == ""||$page <= 0){ $page = 1; } ...
- dbUtils 工具类介绍
导包: commons-dbutils.jar 核心类: QueryRunner 常用方法: // 执行增,删,改语句, 返回影响的行数 int update(String sql,Object... ...
- LinuxCentos系统安装Nginx过程记录
网站服务 想必我们大多数人都是通过访问网站而开始接触互联网的吧.我们平时访问的网站服务就是Web网络服务,一般是指允许用户通过浏览器访问到互联网中各种资源的服务. Web网络服务是一种被动访问的服务程 ...
- Python3.6全栈开发实例[006]
6.检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者. dic = {"k1": "v1v1", " ...
- Python利用subprocess起进程
from multiprocessing import Process, Pool import time import subprocess def task(msg): print 'hello, ...
- openstack ocata版(脚本)计算节点安装
一.初始化环境: 1.安装软件包: yum -y install centos-release-openstack-ocata yum -y upgrade yum -y install python ...
- iOS CMTimeMake 和 CMTimeMakeWithSeconds 学习
CMTime是专门用于标识电影时间的结构体,通常用如下两个函数来创建CMTime (1)CMTimeMake CMTime CMTimeMake ( int64_t value, //表示 当前视频播 ...
- WEB网页专业词汇 汇总
Accessibility 可访问性 accessor properties 存取器属性 addition 加法 aggregate 聚合 alphabetical order 字母表顺序 Anch ...