java list转JSONObject报错
来源:互联网 发布:google cloud sql 编辑:程序博客网 时间:2024/06/06 00:55
最近的项目需要将list转json返回到页面显示,遇到问题:
net.sf.json.JSONException: java.lang.reflect.InvocationTargetExceptionat net.sf.json.JSONObject._fromBean(JSONObject.java:738)at net.sf.json.JSONObject.fromObject(JSONObject.java:182)at net.sf.json.JSONObject._processValue(JSONObject.java:2426)at net.sf.json.JSONObject._setInternal(JSONObject.java:2447)at net.sf.json.JSONObject.setValue(JSONObject.java:1189)at net.sf.json.JSONObject._fromBean(JSONObject.java:725)at net.sf.json.JSONObject.fromObject(JSONObject.java:182)at net.sf.json.JSONArray._processValue(JSONArray.java:2294)at net.sf.json.JSONArray.processValue(JSONArray.java:2325)at net.sf.json.JSONArray.addValue(JSONArray.java:2312)at net.sf.json.JSONArray._fromCollection(JSONArray.java:841)at net.sf.json.JSONArray.fromObject(JSONArray.java:137)at net.sf.json.JSONArray.fromObject(JSONArray.java:120)谷歌了一下,据说是因为list中的数据有java.sql.date类型,而json的转换需要java.util.date。所以转换会报错。如果 list中数据有java.sql.time也会有同样的错。废话不多说,说下怎么解决。JSON jar包提供了扩展JSON转换处理类的方法。扩展类需要实现JsonValueProcessor,代码如下
import java.sql.Time;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import net.sf.json.JsonConfig;import net.sf.json.processors.JsonValueProcessor;public class JsonDateValueProcessor implements JsonValueProcessor { private String format ="yyyy-MM-dd hh:mm:ss"; private String format2 ="hh:mm:ss"; public Object processArrayValue(Object value, JsonConfig config) { return process(value); } public Object processObjectValue(String key, Object value, JsonConfig config) { return process(value); } private Object process(Object value){ if(value instanceof Date){ SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.UK); return sdf.format(value); } if(value instanceof Time){ SimpleDateFormat sdf = new SimpleDateFormat(format2,Locale.UK); return sdf.format(value); } return value == null ? "" : value.toString(); } }
转换JSON的时候如下:
JSONObject jsonArray = new JSONObject();JsonConfig jsonConfig = new JsonConfig();jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor()); jsonConfig.registerJsonValueProcessor(java.sql.Time.class, new JsonDateValueProcessor()); JSONArray jsonObj = JSONArray.fromObject(userlist, jsonConfig);
0 0
- java list转JSONObject报错
- new jsonObject报错
- JSONObject json = JSONObject.fromObject(josnStr);报错
- org.json.JSONObject中list转json
- JSONObject.fromObject方法报错解决方案
- JSONObject.fromObject(map) 执行报错.
- 在servlet中使用json-lib报错--java.lang.NoClassDefFoundError: net/sf/json/JSONObject
- Java中json数据问题.报错 A JSONObject text must begin with '{'
- 使用JSONObject报错:Java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeEx
- Java中List.remove报错UnsupportedOperationException
- Java中list.get(index)报错
- Java中list.get(index)报错
- Java中List.remove报错UnsupportedOperationException
- list转json报错
- Java发送Https请求,返回JSONObject转为List
- JSONObject.java
- Java JSONObject
- List<JSONObject>和JSONArray
- 重构定义
- 欢迎使用CSDN-markdown编辑器
- 数据结构实验之链表九:双向链表
- #2006 - MySQL server has gone away 问题解决方法
- mysql事务隔离级别、乐观锁、悲观锁
- java list转JSONObject报错
- easyui datagrid宽度设置在ie8无效的问题
- jquery easyui window href加载页面,子页面JS失效问题
- 静态重定位和动态重定位
- mysql中将datetime转换为秒数
- ibatis Parameter index out of range (1 > number of parameters, which is 0)
- [Swift]语法基础:for循环的使用
- loginFilter不能过滤*.action
- HDU 1098 Ignatius's puzzle(据说是规律题,然而我们要以正确的姿势搞定它)