Jackson JSON读写JSON字符串

时间:2013-06-26 15:42:59

标签: java android json list mapper

我有一个由一个int组成的类(A)和一个仅由int组成的另一个对象(B)的List。

它们都是相对的getter和setter方法。我可以将我的firts对象正确保存到String中,但是在尝试阅读时我遇到了问题。如果我的对象列表(A)是空的我没有问题,我可以保存并正确读取它,但如果列表不为空我可以正确保存但我无法读取它。为什么??

ObjectMapper mapper = new ObjectMapper();
ObjectA a = mapper.readValue(StringUsedFor, ObjectA.class);

logcat的:

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 input first = 1; B秒=新B(2,5);

仅当秒为空或为空时才能正常运行。

0 个答案:

没有答案