如何使用Java解析嵌套数组与Jackson

时间:2016-10-22 06:08:44

标签: java json jackson unmarshalling

以下是我使用杰克逊转换为POJO的Json:

[
    [
        {
             "title"  : "Professional JavaScript",
             "author" : "Nicholas C. Zakas"
        },
        {
            "title" : "JavaScript: The Definitive Guide",
            "author" : "David Flanagan
        },
    ],
    [
        {
            "title" : "High Performance JavaScript",
            "author" : "Nicholas C. Zakas"
        },
        {
            "title"  : "Professional JavaScript",
            "author" : "Nicholas C. Zakas"
        },
        {
           "title" : "JavaScript: The Definitive Guide",
            "author" : "Nicholas C. Zakas"
        }
    ]
]

我收到以下错误

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.test.sample.jsonsample out of START_ARRAY token

以下是我的班级

public class jsonsample
{
   @JsonProperty("title")
   public String title;
   @JsonProperty("author")
   public String author;
  //getters and setters
}

public class mainclass
{
  public List<jsonsample> sjson;
}

ObjectMapper mapper = new ObjectMapper();
List<mainclass> accs = mapper.readValue(rspString,mapper.getTypeFactory().constructCollectionType(List.class, mainclass.class));

如何从嵌套结构以及未命名的数组中获取数据?

0 个答案:

没有答案