我是JSON的新手,并且在为后续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;
}
}
谢谢
答案 0 :(得分:1)
您的代码实际上真的关闭,有两个错误:
1)您收到的错误告诉您,在JSON的最开始,它期待数组的开始([
),但得到了一个对象的开始({{1}相反。您发布的JSON是而不是您正在为Gson提供什么(见下文)。
2)当您更正上述内容时,{
POJO中存在一个小问题:
Person
应该是:
public ArrayList<Profile> profile;
你实际上不需要你的public ArrayList<Image> profile;
课程; JSON 中的Profile
是数组。你会收到另一个类似于你现在收到的错误,因为它会期待一个profile
个对象的ArrayList:Profile
如果您修复了第二个并为Gson提供了您发布的JSON,那么它的效果非常好。您已经发布了一组"profile":[{"image":[{imageobject},...]},{"image":[{imageobject},...]}]
个对象,但您在代码中实际拥有的内容(在Person
中)只是一个对象...或包含您发布的数组的对象。下面的小例子非常有效(在纠正你的json
课程后):
Person
编辑添加:您实际上正在进行一些Gson正在为您处理的类型转换......但您可能并不真正想要。例如,在您的JSON中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);
}
}
是score
,但在您的POJO中,您已将其设为int
。 Gson默默地进行转换。
另外,像我上面所做的那样简单地创建SSCCE(http://sscce.org/)可以帮助您调试这些问题。