Java 使用Jackson将嵌套数组反序列化为ArrayList

Java 使用Jackson将嵌套数组反序列化为ArrayList,java,arrays,json,jackson,Java,Arrays,Json,Jackson,我有一段JSON,看起来像这样: { "authors": { "author": [ { "given-name": "Adrienne H.", "surname": "Kovacs" }, { "given-name": "Philip", "surname": "Moons" } ] } } // The JSON final String jso

我有一段JSON,看起来像这样:

{
  "authors": {
    "author": [
      {
        "given-name": "Adrienne H.",
        "surname": "Kovacs"
      },
      {
        "given-name": "Philip",
        "surname": "Moons"
      }
    ]
   }
 }
// The JSON
final String json = "{\"authors\":{\"author\":[{\"given-name\":\"AdrienneH.\",\"surname\":\"Kovacs\"},{\"given-name\":\"Philip\",\"surname\":\"Moons\"}]}}";

ObjectMapper mapper = new ObjectMapper();

// Read the response as a tree model
final JsonNode response = mapper.readTree(json).path("authors").path("author");

// Create the collection type (since it is a collection of Authors)
final CollectionType collectionType =
        TypeFactory
                .defaultInstance()
                .constructCollectionType(List.class, Author.class);

// Convert the tree model to the collection (of Author-objects)
List<Author> authors = mapper.reader(collectionType).readValue(response);

// Now the authors-list is ready to use...
我创建了一个类来存储作者信息:

public class Author {
    @JsonProperty("given-name")
    public String givenName;
    public String surname;
}
和两个包装器类:

public class Authors {
    public List<Author> author;
}

public class Response {
    public Authors authors;
}
然后像这样使用它:

@JsonDeserialize(using = AuthorArrayDeserializer.class)
public void setAuthors(List<Author> authors) {
    this.authors = authors;
}
@JsonDeserialize(使用=authorararydeserializer.class)
公共空集合作者(列出作者){
this.authors=作者;
}

感谢@wassgren的想法。

如果您想摆脱包装器类,我看到至少有两种方法可以做到这一点。第一种是使用Jackson树模型(
JsonNode
),第二种是使用名为
UNWRAP\u ROOT\u VALUE
的反序列化功能


备选方案1:使用
JsonNode

使用Jackson反序列化JSON时,有多种方法可以控制要创建的对象类型。
ObjectMapper
可以将JSON反序列化为例如
Map
JsonNode
(通过
readTree
-方法)或POJO

如果将
readTree
-方法与
POJO
转换相结合,可以完全删除包装器。例如:

// The author class (a bit cleaned up)
public class Author {
    private final String givenName;
    private final String surname;

    @JsonCreator
    public Author(
            @JsonProperty("given-name") final String givenName,
            @JsonProperty("surname") final String surname) {

        this.givenName = givenName;
        this.surname = surname;
    }

    public String getGivenName() {
        return givenName;
    }

    public String getSurname() {
        return surname;
    }
}
反序列化可以如下所示:

{
  "authors": {
    "author": [
      {
        "given-name": "Adrienne H.",
        "surname": "Kovacs"
      },
      {
        "given-name": "Philip",
        "surname": "Moons"
      }
    ]
   }
 }
// The JSON
final String json = "{\"authors\":{\"author\":[{\"given-name\":\"AdrienneH.\",\"surname\":\"Kovacs\"},{\"given-name\":\"Philip\",\"surname\":\"Moons\"}]}}";

ObjectMapper mapper = new ObjectMapper();

// Read the response as a tree model
final JsonNode response = mapper.readTree(json).path("authors").path("author");

// Create the collection type (since it is a collection of Authors)
final CollectionType collectionType =
        TypeFactory
                .defaultInstance()
                .constructCollectionType(List.class, Author.class);

// Convert the tree model to the collection (of Author-objects)
List<Author> authors = mapper.reader(collectionType).readValue(response);

// Now the authors-list is ready to use...
然后,对于您的映射器,只需使用:

ObjectMapper mapper = new ObjectMapper();

// Unwrap the root value i.e. the "authors"
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
final Response responsePojo = mapper.readValue(json, Response.class);

第二种方法只删除了一个包装器类,但是解析函数非常漂亮。

答案对您有帮助吗