将json对象反序列化为库类

时间:2014-10-20 04:25:19

标签: java android json gson

我现在正在处理一些Android代码,我需要使用SharedPreferences保存一个对象并且这样做我想我可能会使用Gson将其转换为Json字符串并存储字符串。问题是,当我尝试反序列化时,我会得到一个非正式的参数异常,这个异常是由不能正确反序列化的字段引起的。我想为这个类编写一个自定义的反序列化方法,但该类来自我作为我无权访问的库的API。有任何想法吗?提前谢谢!

编辑:这是logcat中与此问题相对应的文本:

10-20 14:29:42.600: E/AndroidRuntime(28434): FATAL EXCEPTION: main
10-20 14:29:42.600: E/AndroidRuntime(28434): Process: com.example.planit, PID: 28434
10-20 14:29:42.600: E/AndroidRuntime(28434): java.lang.IllegalArgumentException: invalid value for field
10-20 14:29:42.600: E/AndroidRuntime(28434):    at java.lang.reflect.Field.setField(Native Method)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at java.lang.reflect.Field.set(Field.java:585)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at com.google.api.client.util.FieldInfo.setFieldValue(FieldInfo.java:245)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at com.google.api.client.util.FieldInfo.setValue(FieldInfo.java:206)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at com.google.api.client.util.GenericData.put(GenericData.java:103)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at com.google.api.client.util.GenericData.put(GenericData.java:47)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:189)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:146)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at com.google.gson.Gson.fromJson(Gson.java:755)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at com.google.gson.Gson.fromJson(Gson.java:721)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at com.google.gson.Gson.fromJson(Gson.java:670)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at com.google.gson.Gson.fromJson(Gson.java:642)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at com.example.planit.TabbedActivity.getPlanItID(TabbedActivity.java:34)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at com.example.planit.FriendsSectionFragment.onCreate(FriendsSectionFragment.java:39)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at android.app.Fragment.performCreate(Fragment.java:1678)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at android.os.Handler.handleCallback(Handler.java:733)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at android.os.Handler.dispatchMessage(Handler.java:95)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at android.os.Looper.loop(Looper.java:136)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at android.app.ActivityThread.main(ActivityThread.java:5001)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at java.lang.reflect.Method.invokeNative(Native Method)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at java.lang.reflect.Method.invoke(Method.java:515)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-20 14:29:42.600: E/AndroidRuntime(28434):    at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案