JSON解析转化list对象为String时出错

来源:互联网 发布:流量攻击软件 编辑:程序博客网 时间:2024/05/01 00:17

花了将近两个小时解决这个问题。幸亏认真分析了所有报出的异常。

出问题的地方是这样的:客户端通过ajax访问servlet,servlet要调用service从数据库取出一个list<goods>,然后通过JSONArray将list转为String传回客户端。这时候servlet内的JSONArray.fromObject().toString();这个地方报错,主要详情如下,我的servlet是通过反射调用执行的方法。

Caused by: net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
    at net.sf.json.JSONObject._fromBean(JSONObject.java:959)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:194)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2793)
    at net.sf.json.JSONObject._setInternal(JSONObject.java:2817)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1527)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:946)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:194)
    at net.sf.json.JSONArray._processValue(JSONArray.java:2562)
    at net.sf.json.JSONArray.processValue(JSONArray.java:2593)
    at net.sf.json.JSONArray.addValue(JSONArray.java:2580)
    at net.sf.json.JSONArray._fromCollection(JSONArray.java:1084)
    at net.sf.json.JSONArray.fromObject(JSONArray.java:147)
    at net.sf.json.JSONArray.fromObject(JSONArray.java:129)
    at com.jd.servlet.RecommendServlet.goodsExpress(RecommendServlet.java:28)
    ... 31 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1773)
    at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1132)
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:686)
    at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715)
    at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:290)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:931)
    ... 44 more
Caused by: java.lang.IllegalArgumentException
    at java.sql.Date.getHours(Date.java:182)
    ... 54 more
研究半天发现问题是JSON不支持java.sql.date类型数据,我的goods里使用了这个数据类型。

解决方案当然就是通过 new java.util.date(sqldate.getTime());  以及new java.sql.date(utildate.getTime());来进行数据转换!

0 0
原创粉丝点击