问题描述
我刚开始到Android prograamming,并通过使用IMDB API找到很好的教程。而不是在本教程中使用的XML我想用JSON并为recevied JSON我有一个问题。
这是person.json:
i just started to android prograamming and found nice tutorial by using imdb api. instead of using xml in this tutorial i would like to use json and for the recevied json i have a problem.this is the person.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"
}
]
我的两个对象,为他们:
my two object for them:
public class Person implements Serializable {
private static final long serialVersionUID = 6794898677027141412L;
public String score;
public String popularity;
public String name;
public String id;
public String biography;
public String url;
public String version;
public String lastModifiedAt;
public Profile profile;
}
public class Profile implements Serializable {
private static final long serialVersionUID = -8735669377345509929L;
public ArrayList<Image> imagesList;
}
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;
}
我不能想出如何利用杰克逊对象映射器检索人员列表。
当我使用这一个:
ı couldnt figure out how to retrieve person list by using jackson object mapper.when i use this one:
ObjectMapper mapper = new ObjectMapper();
Person person= mapper.readValue(jsonResponseString, Person.class);
我得到:
18 02-16:34:48.010:W / System.err的(376):com.fasterxml.jackson.databind.JsonMappingException:无法反序列化com.example.imdbsearcher.model.Person实例出来START_ARRAY令牌
02-16 18:34:48.180:W / System.err的(376):在[来源:java.io.StringReader@40a81778;行:1,柱:1]
02-16 18:34:48.554:W / System.err的(376):在com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:599)
02-16 18:34:48.830:W / System.err的(376):在com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:593)
02-16 18:34:48.010: W/System.err(376): com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.example.imdbsearcher.model.Person out of START_ARRAY token02-16 18:34:48.180: W/System.err(376): at [Source: java.io.StringReader@40a81778; line: 1, column: 1]02-16 18:34:48.554: W/System.err(376): at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:599)02-16 18:34:48.830: W/System.err(376): at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:593)
我已经改变了与Keith和crdnilfan的意见检索方法。
但现在我有轮廓的属性问题。
i have changed the retrieve method with advice of Keith and crdnilfan.but now i have a problem with the attribute of profile.
我意识到,我很想念一个人,在对象basicly我已经创造了新的配置文件对象和移动图像列表此类。
i realized that i am missing that one in person object and basicly i have created new profile object and moved imageList to this class.
我已经更新POJO的方法同上。
i have updated POJO's as above.
但现在我得到同样的错误的配置文件。
but now i am getting the same error for the profile.
无法反序列化com.example.imdbsearcher.model.Profile实例出来START_ARRAY令牌
Can not deserialize instance of com.example.imdbsearcher.model.Profile out of START_ARRAY token
推荐答案
您需要反序列化的列表,你JSON是一个数组:
You need to deserialize the list, as your JSON is an array:
List<Person> people = mapper.readValue(
jsonResponseString, new TypeReference<List<Person >>(){});
然而,在你做,你将不得不因为在你的JSON配置文件属性的一些额外的反序列化的错误。结帐:<一href=\"http://jackson.$c$chaus.org/1.5.7/javadoc/org/$c$chaus/jackson/annotate/JsonIgnoreProperties.html\" rel=\"nofollow\">http://jackson.$c$chaus.org/1.5.7/javadoc/org/$c$chaus/jackson/annotate/JsonIgnoreProperties.html
更新:
public class Person implements Serializable
{
public List<Object> profile;
public String score;
public String popularity;
public String name;
public String id;
public String biography;
public String url;
public String version;
public String last_modified_at;
}
有几种方法来处理这个问题。这会给你的个人资料链接的哈希映射。
There are several ways to deal with this. This will give you a linked hash map for Profile.
另外,如果你控制了JSON格式,配置文件的语法改成这样:
Alternatively, if you control the JSON format, change the profile syntax to this:
"profile":[
image:...,
image:...
]
这篇关于嵌套的JSON解析与杰克逊的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!