我有一个由 int 组成的 i 类 (A) 和一个仅由 int 组成的我的另一个对象 (B) 的列表。
它们都是相对的 getter 和 setter 方法。我可以正确地将我的第一个对象保存到一个字符串中,但是我在尝试阅读它时遇到了问题。如果我的对象(A)的列表是空的,我没有问题,我可以保存并正确读取它,但如果列表不为空,我可以正确保存它但我无法读取它。为什么??
ObjectMapper mapper = new ObjectMapper();
ObjectA a = mapper.readValue(StringUsedFor, ObjectA.class);
日志猫:
06-26 15:51:38.723: W/System.err(825): org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class core.B]: can not instantiate from JSON object (need to add/enable type information?)
06-26 15:51:38.723: W/System.err(825): at [Source: java.io.StringReader@41223378; line: 1, column: 12] (through reference chain: core.ObjectA["b"])
06-26 15:51:38.763: W/System.err(825): at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:746)
06-26 15:51:38.763: W/System.err(825): at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:683)
06-26 15:51:38.763: W/System.err(825): at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
06-26 15:51:38.773: W/System.err(825): at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
06-26 15:51:38.773: W/System.err(825): at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:194)
06-26 15:51:38.773: W/System.err(825): at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30)
06-26 15:51:38.773: W/System.err(825): at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
06-26 15:51:38.773: W/System.err(825): at org.codehaus.jackson.map.deser.SettableBeanProperty$FieldProperty.deserializeAndSet(SettableBeanProperty.java:579)
06-26 15:51:38.773: W/System.err(825): at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
06-26 15:51:38.783: W/System.err(825): at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
06-26 15:51:38.783: W/System.err(825): at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
06-26 15:51:38.783: W/System.err(825): at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863)
06-26 15:51:38.793: W/System.err(825): at com.a.MainActivity.readLibretto(MainActivity.java:145)
06-26 15:51:38.813: W/System.err(825): at com.a.MainActivity.onCreate(MainActivity.java:40)
06-26 15:51:38.813: W/System.err(825): at android.app.Activity.performCreate(Activity.java:5008)
06-26 15:51:38.813: W/System.err(825): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-26 15:51:38.823: W/System.err(825): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-26 15:51:38.823: W/System.err(825): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-26 15:51:38.834: W/System.err(825): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-26 15:51:38.834: W/System.err(825): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-26 15:51:38.844: W/System.err(825): at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 15:51:38.844: D/dalvikvm(825): GC_CONCURRENT freed 385K, 7% free 8585K/9223K, paused 21ms+51ms, total 167ms
06-26 15:51:38.854: W/System.err(825): at android.os.Looper.loop(Looper.java:137)
06-26 15:51:38.854: W/System.err(825): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-26 15:51:38.854: W/System.err(825): at java.lang.reflect.Method.invokeNative(Native Method)
06-26 15:51:38.864: W/System.err(825): at java.lang.reflect.Method.invoke(Method.java:511)
06-26 15:51:38.864: W/System.err(825): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-26 15:51:38.864: W/System.err(825): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-26 15:51:38.874: W/System.err(825): at dalvik.system.NativeStart.main(Native Method)
数据输入示例:
int 输入第一 = 1; B 秒 = 新 B(2,5);
仅当 second 为空或 null 时才能正确运行。