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

來源:互聯網
上載者:User

標籤:

注:在項目中, 我使用原生的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 moreCaused by: java.lang.IllegalArgumentException    at java.sql.Date.getHours(Date.java:182)    ... 47 more
View Code

該問題描述了使用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();    }}

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.