2013-02-16 58 views
1

我刚刚开始android程序编写和发现很好的教程通过使用imdb api。而不是在本教程中使用XML,我想使用JSON和收到的JSON我有一个问题。 这是person.json:嵌套JSON解析与杰克逊安卓android

[ 
     { 
    "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 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; 
    } 

我不能想出如何利用杰克逊对象映射检索人名单。 当我使用这一个:

ObjectMapper mapper = new ObjectMapper(); 
    Person person= mapper.readValue(jsonResponseString, Person.class); 

我得到:

02-16 18: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):at [Source:[email protected]; 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)

我已经改变了检索方法的建议基思和crdnilfan 。 但现在我有配置文件的属性问题。

我意识到我错过了一个人对象,基本上我创建了新的配置文件对象,并将imageList移动​​到这个类。

我已经更新了POJO的如上。

但现在我得到的配置文件相同的错误。

无法反序列化com.example.imdbsearcher.model.Profile的情况下进行START_ARRAY令牌

+0

邮报满请跟踪stacktrace。 – nhaarman 2013-02-16 18:25:14

+0

我已经添加了该类的修改版本到我的回答 – 2013-02-16 21:18:34

回答

2

你需要反序列化的列表,你JSON是一个数组:

List<Person> people = mapper.readValue(
         jsonResponseString, new TypeReference<List<Person >>(){}); 

但是,你做,你以后由于JSON中的profile属性,会有一些额外的反序列化错误。结帐:http://jackson.codehaus.org/1.5.7/javadoc/org/codehaus/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; 
} 

有几种方法来解决这个问题。这会给你一个配置文件的链接散列图。

或者,如果你控制了JSON格式,配置文件的语法改成这样:

"profile":[ 
     image:..., 
     image:... 
] 
+0

谢谢基思,但我仍然不知道在这个映射,杰克逊将如何映射图像属性? – 2013-02-16 21:30:12

+0

好吧我认为这将公开名单个人资料在我的情况,不是吗? – 2013-02-16 21:31:33

+0

您的JSON只包含“image”属性的单个对象,而不是JSON数组。所以它应该只是'Image'对象。 – StaxMan 2013-02-18 06:38:52

2

那是因为你试图反序列化Person.class的列表,而不是一个实例。创建另一个类这样

public class PersonList extends ArrayList<Person> {} 

然后用

ArrayList<Person> people = mapper.readValue(jsonResponseString, PersonList.class); 
+2

那,或者映射到数组:[Person [] people = mapper.readValue(jsonResponseString,Person []。class);'(数组相当不错,因为它们没有泛型的强类型) – StaxMan 2013-02-18 06:39:51

0

其他两个答案清楚地解释了错误的原因,它会摆脱掉错误,它会解析人物。但对我来说,它没有解析图像对象。下面POJO,我们甚至可以在没有

@JsonIgnoreProperties 

or 

mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); 

POJO定义的完全解析指定的JSON,没有任何问题:

public class PersonList extends ArrayList<Person> {} 

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; 
    @JsonProperty(value="last_modified_at") 
    public String lastModifiedAt; 

    public List<Profile> profile; 

} 

public class Profile implements Serializable { 

private static final long serialVersionUID = -8735669377345509929L; 
    @JsonProperty(value="image") 
    public Image imagesList; 

} 

public class Image implements Serializable { 

    private static final long serialVersionUID = -2428562977284114465L; 

    public String id; 
    public String type; 
    public String url; 
    public String size; 
    public int width; 
    public int height; 

} 

这应该解析JSON

String jsonResponseString = readJsonFile("person.json"); 
ObjectMapper mapper = new ObjectMapper(); 

PersonList person= mapper.readValue(jsonResponseString, PersonList.class); 
     or 
List<Person> person= mapper.readValue(jsonResponseString, 
new TypeReference<List< Person>>(){}); //if we use this we dont have to define PersonList class