嵌套json的POJO
问题描述:
我是JSON中的新成员,并且在为后续的JSON输入创建POJO时遇到问题。嵌套json的POJO
[
{
"score":1,
"popularity":3,
"name":"Brad Pitt",
"id":287,
"biography":"test",
"url":"http://www.themoviedb.org/person/287",
"profile":[
{
"image":{
"type":"profile",
"size":"thumb",
"height":68,
"width":45,
"url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w45/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg",
"id":"4ea5cb8c2c0588394800006f"
}
},
{
"image":{
"type":"profile",
"size":"profile",
"height":281,
"width":185,
"url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg",
"id":"4ea5cb8c2c0588394800006f"
}
},
{
"image":{
"type":"profile",
"size":"h632",
"height":632,
"width":416,
"url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/h632/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg",
"id":"4ea5cb8c2c0588394800006f"
}
},
{
"image":{
"type":"profile",
"size":"original",
"height":1969,
"width":1295,
"url":"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg",
"id":"4ea5cb8c2c0588394800006f"
}
}
],
"version":685,
"last_modified_at":"2013-02-16 07:11:15 UTC"
}
]
我已经创建了三个POJO对他们的这个人是主要负责人类这其中蕴含配置文件对象也。简介对象返回图像列表:
public class Person implements Serializable {
private static final long serialVersionUID = 6794898677027141412L;
public String biography;
public String id;
public String last_modified_at;
public String name;
public String popularity;
public String score;
public String url;
public String version;
public ArrayList<Profile> profile;
//getters and setters
}
public class Profile implements Serializable {
private static final long serialVersionUID = -6482559829789740926L;
public ArrayList<Image> image;
}
public class Image implements Serializable {
private static final long serialVersionUID = -2428562977284114465L;
public String type;
public String url;
public String size;
public int width;
public int height;
}
我使用GSON检索数据:
Gson gson = new Gson();
Type collectionType = new TypeToken<List<Person>>(){}.getType();
List<Person> details = gson.fromJson(json, collectionType);
当我运行此我得到:
02-17 11:04:50.531: E/AndroidRuntime(405): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 3
更新图片POJO:
public class Image implements Serializable {
private static final long serialVersionUID = -2428562977284114465L;
private Object image;
public class ImageInner {
public static final String SIZE_ORIGINAL = "original";
public static final String SIZE_MID = "mid";
public static final String SIZE_COVER = "cover";
public static final String SIZE_THUMB = "thumb";
public static final String TYPE_PROFILE = "profile";
public static final String TYPE_POSTER = "poster";
public String type;
public String url;
public String size;
public int width;
public int height;
}
}
谢谢
答
你的代码实际上是真的密切,有两个错误:
1)您收到是告诉你,在JSON的一开始就期待一个数组开始时的错误([
),但取而代之的是一个对象的开始({
)。你发布的JSON是而不是你正在给Gson喂食(见下文)。
2)当你纠正上述情况,你有你的Person
POJO一个小问题:
public ArrayList<Profile> profile;
应该是:
public ArrayList<Image> profile;
你其实并不需要你Profile
类; profile
中的JSON 是的数组。 AS-是你会收到类似你现在收到什么其他错误,因为它会被期待Profile
对象的ArrayList:"profile":[{"image":[{imageobject},...]},{"image":[{imageobject},...]}]
如果固定两个号码和饲料GSON您已发布的JSON,它完美的作品。你已经发布了一个Person
对象的数组,但是实际上代码中的内容(在json
中)只是一个对象...或包含已发布数组的对象。下面的小例子作品完美(矫正你的Person
课后):
public class App
{
static String json = "[{\"score\":1,\"popularity\":3,\"name\":\"Brad Pitt\",\"id\":287," +
"\"biography\":\"test\",\"url\":\"http://www.themoviedb.org/person/287\",\"profile\":" +
"[{\"image\":{\"type\":\"profile\",\"size\":\"thumb\",\"height\":68,\"width\":45,\"url\":" +
"\"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w45/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg\"," +
"\"id\":\"4ea5cb8c2c0588394800006f\"}},{\"image\":{\"type\":\"profile\",\"size\":\"profile\"," +
"\"height\":281,\"width\":185,\"url\":\"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg\"," +
"\"id\":\"4ea5cb8c2c0588394800006f\"}},{\"image\":{\"type\":\"profile\",\"size\":\"h632\",\"height\":632,\"width\":416,"+
"\"url\":\"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/h632/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg\"," +
"\"id\":\"4ea5cb8c2c0588394800006f\"}},{\"image\":{\"type\":\"profile\",\"size\":\"original\",\"height\":1969," +
"\"width\":1295,\"url\":\"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg\"," +
"\"id\":\"4ea5cb8c2c0588394800006f\"}}],\"version\":685,\"last_modified_at\":\"2013-02-16 07:11:15 UTC\"" +
"}]";
public static void main(String[] args)
{
Gson gson = new Gson();
Type collectionType = new TypeToken<List<Person>>(){}.getType();
List<Person> details = gson.fromJson(json, collectionType);
}
}
编辑补充:你确实有某种类型的转换将会对GSON正在处理你...但你可能不是真的想。例如,在您的JSON score
是int
,但在您的POJO中,您已将其设置为String
。 Gson默默地进行转换。
此外,只需创建一个SSCCE(http://sscce.org/)就像我以上所做的那样可以帮助您调试这些问题。
嗨布莱恩,我会检查收到的JSON并更新人POJO如你所述。当我尝试时,我会提供信息。谢谢 – 2013-02-18 12:26:51
谢谢你现在完美。 – 2013-02-18 20:59:50
嗨布莱恩,我想我错过了这个答案中的一点,因为即使图像属性填充,当我得到人的列表观察到所有的图像属性为空。你有什么想法吗? – 2013-02-20 07:38:33