使用GSON反序列化非泛型类型的ArrayList
编辑:空指针是由于格式错误的 json 造成的。
您好,
我正在尝试将 json 数据反序列化为 Restaurant 对象的 ArrayList,如下所示(受我在 https://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples):
Type listType = new TypeToken<ArrayList<Restaurant>>() {}.getType();
ArrayList<Restaurant> objList = gson.fromJson( r, listType ); //(line 141)
但这给出了显示的错误在帖子的最后。
看来listType中有一些空指针...这是listType的手表:
"listType"= ImplForType (id=830061042288)
args= ListOfTypes (id=830061040752)
list= null
resolvedTypes= Type[1] (id=830061042800)
loader= PathClassLoader (id=830060323064)
ownerType0= null
ownerTypeRes= null
rawType= Class (java.util.ArrayList) (id=830002592616)
rawTypeName= "java.util.ArrayList" (id=830061042328)
我的代码出了什么问题?我怎样才能做到这一点?
谢谢
七月
07-06 17:38:48.825: DEBUG/dalvikvm(1337): JDWP invocation returning with exceptObj=0x4364a4a0
07-06 17:39:00.525: DEBUG/dalvikvm(1337): JDWP invocation returning with exceptObj=0x4364b1e8
07-06 17:39:15.879: WARN/dalvikvm(1337): threadid=17: thread exiting with uncaught exception (group=0x4000fe70)
07-06 17:39:15.885: ERROR/AndroidRuntime(1337): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
07-06 17:39:16.135: DEBUG/dalvikvm(1337): GC freed 3322 objects / 261128 bytes in 114ms
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): java.lang.RuntimeException: An error occured while executing doInBackground()
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:234)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:258)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.util.concurrent.FutureTask.run(FutureTask.java:122)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.lang.Thread.run(Thread.java:1058)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): Caused by: java.lang.NullPointerException
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at org.apache.harmony.luni.lang.reflect.ListOfTypes.length(ListOfTypes.java:47)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at org.apache.harmony.luni.lang.reflect.ImplForType.toString(ImplForType.java:83)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.lang.StringBuilder.append(StringBuilder.java:209)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:56)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:65)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:96)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:49)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at com.google.gson.Gson.fromJson(Gson.java:379)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at org.digitalfarm.atable.Atable$GetRestaurantData.doInBackground(Atable.java:141)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at org.digitalfarm.atable.Atable$GetRestaurantData.doInBackground(Atable.java:1)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): ... 4 more
EDIT: the null pointer was due to a badly formed json.
Hi,
I'm trying to deserialize json data to an ArrayList of Restaurant object as follows (inpsired by what I found in https://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples):
Type listType = new TypeToken<ArrayList<Restaurant>>() {}.getType();
ArrayList<Restaurant> objList = gson.fromJson( r, listType ); //(line 141)
but this gives the error shown at the end of the post.
It seems that there is some null pointer in listType... Here's a watch of listType:
"listType"= ImplForType (id=830061042288)
args= ListOfTypes (id=830061040752)
list= null
resolvedTypes= Type[1] (id=830061042800)
loader= PathClassLoader (id=830060323064)
ownerType0= null
ownerTypeRes= null
rawType= Class (java.util.ArrayList) (id=830002592616)
rawTypeName= "java.util.ArrayList" (id=830061042328)
What's wrong with my code? How can I do that?
Thanks
Jul
07-06 17:38:48.825: DEBUG/dalvikvm(1337): JDWP invocation returning with exceptObj=0x4364a4a0
07-06 17:39:00.525: DEBUG/dalvikvm(1337): JDWP invocation returning with exceptObj=0x4364b1e8
07-06 17:39:15.879: WARN/dalvikvm(1337): threadid=17: thread exiting with uncaught exception (group=0x4000fe70)
07-06 17:39:15.885: ERROR/AndroidRuntime(1337): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
07-06 17:39:16.135: DEBUG/dalvikvm(1337): GC freed 3322 objects / 261128 bytes in 114ms
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): java.lang.RuntimeException: An error occured while executing doInBackground()
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:234)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:258)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.util.concurrent.FutureTask.run(FutureTask.java:122)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.lang.Thread.run(Thread.java:1058)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): Caused by: java.lang.NullPointerException
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at org.apache.harmony.luni.lang.reflect.ListOfTypes.length(ListOfTypes.java:47)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at org.apache.harmony.luni.lang.reflect.ImplForType.toString(ImplForType.java:83)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.lang.StringBuilder.append(StringBuilder.java:209)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:56)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:65)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:96)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:49)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at com.google.gson.Gson.fromJson(Gson.java:379)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at org.digitalfarm.atable.Atable$GetRestaurantData.doInBackground(Atable.java:141)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at org.digitalfarm.atable.Atable$GetRestaurantData.doInBackground(Atable.java:1)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256)
07-06 17:39:16.225: ERROR/AndroidRuntime(1337): ... 4 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我们在姜饼中修复的一个已知错误;请参阅 http://code.google.com/p/android/issues /detail?id=6636
this is a known bug that we've fixed in gingerbread; see http://code.google.com/p/android/issues/detail?id=6636