首页

通过flexjson将JSON字符串转换为对象报“flexjson.JSONException: [ id ]: Cannot instantiate abstract class or interface int”

标签:flexjson,异常,error,exception,JSONDeserializer,JSONSerializer,JSONException     发布时间:2017-05-29   

一、异常描述

通过flexjson包将字符串转换为对象(具体代码示例详情参加关联页)时报Exception in thread "main" flexjson.JSONException: [ id ]:  Cannot instantiate abstract class or interface int  或  [ id ]: java.lang.Integer lacks a no argument constructor.  Flexjson will instantiate any protected, private, or public no-arg constructor. 等异常,具体如下所示

Exception in thread "main" flexjson.JSONException: [ id ]:  Cannot instantiate abstract class or interface int@b@	at flexjson.ObjectBinder.convert(ObjectBinder.java:172)@b@	at flexjson.ObjectBinder.bindIntoObject(ObjectBinder.java:136)@b@	at flexjson.factories.ClassLocatorObjectFactory.instantiate(ClassLocatorObjectFactory.java:41)@b@	at flexjson.ObjectBinder.bind(ObjectBinder.java:68)@b@	at flexjson.ObjectBinder.bind(ObjectBinder.java:60)@b@	at flexjson.JSONDeserializer.deserialize(JSONDeserializer.java:156)@b@	at com.xwood.publisher.test.JsonUtil.fromJson(JsonUtil.java:48)@b@	at com.xwood.publisher.test.JsonUtil.main(JsonUtil.java:90)@b@	.....@b@	@b@Exception in thread "main" flexjson.JSONException: [ id ]: java.lang.Integer lacks a no argument constructor.  Flexjson will instantiate any protected, private, or public no-arg constructor.@b@	at flexjson.factories.BeanObjectFactory.instantiate(BeanObjectFactory.java:25)@b@	at flexjson.ObjectBinder.convert(ObjectBinder.java:170)@b@	at flexjson.ObjectBinder.bindIntoObject(ObjectBinder.java:136)@b@	at flexjson.factories.ClassLocatorObjectFactory.instantiate(ClassLocatorObjectFactory.java:41)@b@	at flexjson.ObjectBinder.bind(ObjectBinder.java:68)@b@	at flexjson.ObjectBinder.bind(ObjectBinder.java:60)@b@	at flexjson.JSONDeserializer.deserialize(JSONDeserializer.java:156)@b@	at com.xwood.publisher.test.JsonUtil.fromJson(JsonUtil.java:46)@b@	at com.xwood.publisher.test.JsonUtil.main(JsonUtil.java:87)@b@Caused by: java.lang.NoSuchMethodException: java.lang.Integer.<init>()@b@	at java.lang.Class.getConstructor0(Class.java:2715)@b@	at java.lang.Class.getDeclaredConstructor(Class.java:1987)@b@	at flexjson.factories.BeanObjectFactory.instantiate(BeanObjectFactory.java:30)@b@	at flexjson.factories.BeanObjectFactory.instantiate(BeanObjectFactory.java:16)@b@	... 8 more

二、解决方法

将对象类JsonObj的id的数据类型int或Integer改为String

通过flexjson将JSON字符串转换为对象报“flexjson.JSONException: [ id ]:  Cannot instantiate abstract class or interface int”