Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 491|回复: 0

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

[复制链接]
  • TA的每日心情
    奋斗
    3 天前
  • 签到天数: 789 天

    [LV.10]以坛为家III

    2049

    主题

    2107

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    722638
    发表于 2021-5-31 10:28:10 | 显示全部楼层 |阅读模式

    注:在项目中, 我使用原生的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
    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();
        }
    }

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-9-9 22:13 , Processed in 1.020931 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表