json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不给我们报错,很郁闷, 特别是ie, 有些问题, 得借助FireFox的返回结果分析.

当然, FireFox有时也没报错.

异常栈:

net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
    at net.sf.json.JSONObject._fromBean(JSONObject.java:953)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:192)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2774)
    at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)
    at net.sf.json.JSONObject.setValue(JSONObject.java:1507)
    at net.sf.json.JSONObject._fromBean(JSONObject.java:940)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:192)
    at net.sf.json.JSONObject._processValue(JSONObject.java:2774)
    at net.sf.json.JSONObject.processValue(JSONObject.java:2833)
    at net.sf.json.JSONObject.element(JSONObject.java:1871)
    at net.sf.json.JSONObject.element(JSONObject.java:1849)
    at net.sf.json.JSONObject.put(JSONObject.java:2466)
    at sys_bd.json.JsonService.getJsonStr(JsonService.java:8)
    at com.bd.action.LockAction.queryMapByAttSuccess(LockAction.java:442)
    at com.db.action.LockActionTest.testEncapsulateLockAsJson(LockActionTest.java:40)
    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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
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:928)
    ... 37 more
Caused by: java.lang.IllegalArgumentException
    at java.sql.Date.getHours(Date.java:182)
    ... 47 more

该问题描述了使用json封装对象的时候, 对于日期格式,报该错.

有的人说将对象的getter指定包(java.uti.Date)就可以, 但是我测试发现不可以.

我的解决方案是参考: http://segmentfault.com/q/1010000000095526

工具类为:使用config对象

package sys_bd.json;

import java.text.SimpleDateFormat;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class JsonService {
    private JsonService() {}

    private static final JsonConfig jsonConfig = init();

    private static JsonConfig init() {
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(java.util.Date.class,
                new JsonValueProcessor() {
                    private SimpleDateFormat sd = new SimpleDateFormat(
                            "yyyy-MM-dd");

                    public Object processObjectValue(String key, Object value,
                            JsonConfig jsonConfig) {
                        return value == null ? "" : sd.format(value);
                    }

                    public Object processArrayValue(Object value,
                            JsonConfig jsonConfig) {
                        return null;
                    }
                });
        return jsonConfig;
    }

    public static String getJsonStr(String key, Object value) {
        //使用config对象.
        JSONObject jsonObject = JSONObject.fromObject(value, jsonConfig);
        return jsonObject.toString();
    }
}
时间: 2024-10-12 23:37:05

json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException的相关文章

json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

1 //存储Product对象的集合是从,Product是从mysql数据库中查询并添加的 2 ArrayList<Product> list = new ArrayList<Product>(); 3 4 //设置响应对象编码 5 response.setCharacterEncoding("utf-8"); 6 response.setContentType("text/html;charset=utf-8"); 7 8 //将list集

ajax请求json数据异常:nested exception is net.sf.json.JSONException: java.lang.reflect.InvocationTargetException] with root cause

ajax请求json数据异常:nested exception is net.sf.json.JSONException: java.lang.reflect.InvocationTargetException] with root cause 1.异常原因:所请求的json数据中包含java.util.date数据类型,但是在后台并没有将其格式转换 2.解决方法:添加工具类DateJsonValueProcessor import java.text.SimpleDateFormat; imp

含有Date类型属性的实体类转化为JSONArray时报net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

当我们通过session传递数据的时候我通常是: 直接在DAO里从数据库取出含Date类型的数据,而且通常不会将java.sql.Date转为java.util.Date. 这时候前台和后台都不会有任何问题,所以我们以为这样是正常的操作. 但是当我们用JSON传递数据的时候就会发现控制台会报一下错误信息: net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 这时候我们可以再DAO里将java.sql.Dat

org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException异常解决

org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:246) org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:178) org.apache.struts2.json.JSONWriter.p

net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

private java.sql.Date startDate; public java.sql.Date getStartDate() { return startDate; } public void setStartDate(java.sql.Date startDate) { this.startDate = startDate; }

SpringBoot项目启动报错:java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

.   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Sprin

java.lang.reflect.invocationtargetexception

bout Sotirios-Efstathios Maneas Sotirios-Efstathios (Stathis) Maneas is a postgraduate student at the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. His main interests include distributed syste

session.createQuery()不执行和java.lang.reflect.InvocationTargetException

今天写SSH的工程的时候,执行到一个DAO中的Query query = session.createQuery(hql)的时候,没有成功执行,直接跳到了finally,然后前台报了500和java.lang.reflect.InvocationTargetException的错误. 代码如下: package dao; import java.util.List; import model.AccountInfo; import org.hibernate.HibernateException

HTTP Status 500 - java.lang.reflect.InvocationTargetException

type Exception report message java.lang.reflect.InvocationTargetException description The server encountered an internal error that prevented it from fulfilling this request. exception java.lang.RuntimeException: java.lang.reflect.InvocationTargetExc