我如何反序列化JSON对象中的数组? [英] How can I deserialize an array inside a JSON object?
问题描述
我无法解决如何使用Gson反序列化JSON对象内的数组。我尝试反序列化的json对象如下所示:
I can't work out how to deserialize an array inside a JSON object using Gson. The json object that i'm trying to deserialize looks like this:
{"item0":3,
"item1":1,
"item2":3,
"array":[
{"arrayItem1":321779321,
"arrayItem2":"asdfafd",
"arrayItem3":"asasdfadf"}]}
我设法构建一个类似于:
I manage to build a class that looks like this:
public class Watchlist {
private int itemn0;
private int itemn1;
private int itemn2;
private Object array;
}
但是,当gson试图反序列化数组时,它会抛出一个异常:
But when gson tries to deserialize the array it throws an exception:
com.google.gson.JsonParseException: Type information is unavailable, and the target object is not a primitive: <my gson array>
有人可以告诉我如何反序列化这个吗?
Can someone please tell me how to deserialize this?
推荐答案
你的数组是一个数组。
所以在watchList类中
your "array" is an Array. so in watchList class
public class Watchlist {
private int itemn0;
private int itemn1;
private int itemn2;
private List<watchListarray> array;
//constructor
//getter & setter of all
}
now watchListarray class
public class watchListarray{
String arrayItem1="";
String arrayItem2="";
String arrayItem3="";
//constructor
//getter & setters of all
}
现在使用下载Gson
参考: http://primalpop.wordpress.com/2010/06 / 05 / parsing-json-using-gson-in-android /
Now use download Gson refer: http://primalpop.wordpress.com/2010/06/05/parsing-json-using-gson-in-android/
这篇关于我如何反序列化JSON对象中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!