Jackson – 將對象解構到集合/數組中

Data,Jackson,Java Collections
Remote
1
01:32 AM · Dec 01 ,2025

1. 概述

本教程將演示如何使用 Jackson 2 將 JSON 數組反序列化為 Java 數組或集合。

如果您想深入瞭解 Jackson 2 可以執行的其他酷炫功能 – 請訪問主 Jackson 教程。

2. 將對象反序列化為數組

Jackson 可以輕鬆地反序列化為 Java 數組:

@Test
public void givenJsonArray_whenDeserializingAsArray_thenCorrect()
  throws JsonParseException, JsonMappingException, IOException {

    ObjectMapper mapper = new ObjectMapper();
    List listOfDtos = Lists.newArrayList(
      new MyDto("a", 1, true), new MyDto("bc", 3, false));
    String jsonArray = mapper.writeValueAsString(listOfDtos);

    // [{"stringValue":"a","intValue":1,"booleanValue":true},
    // {"stringValue":"bc","intValue":3,"booleanValue":false}]

    MyDto[] asArray = mapper.readValue(jsonArray, MyDto[].class);
    assertThat(asArray[0], instanceOf(MyDto.class));
}

3. 將 JSON 數組反序列化為集合

將相同的 JSON 數組讀取到 Java 集合中比較困難——默認情況下,Jackson 不會獲得完整的泛型類型信息,而是會創建一個 LinkedHashMap 實例的集合:

@Test
public void givenJsonArray_whenDeserializingAsListWithNoTypeInfo_thenNotCorrect()
  throws JsonParseException, JsonMappingException, IOException {

    ObjectMapper mapper = new ObjectMapper();

    List listOfDtos = Lists.newArrayList(
      new MyDto("a", 1, true), new MyDto("bc", 3, false));
    String jsonArray = mapper.writeValueAsString(listOfDtos);

    List asList = mapper.readValue(jsonArray, List.class);
    assertThat((Object) asList.get(0), instanceOf(LinkedHashMap.class));
}

有兩點可以幫助 Jackson 瞭解正確的類型信息——我們可以使用庫中為此目的提供的 TypeReference

@Test
public void givenJsonArray_whenDeserializingAsListWithTypeReferenceHelp_thenCorrect()
  throws JsonParseException, JsonMappingException, IOException {

    ObjectMapper mapper = new ObjectMapper();

    List listOfDtos = Lists.newArrayList(
      new MyDto("a", 1, true), new MyDto("bc", 3, false));
    String jsonArray = mapper.writeValueAsString(listOfDtos);

    List asList = mapper.readValue(
      jsonArray, new TypeReference>() {});
    assertThat(asList.get(0), instanceOf(MyDto.class));
}

或者,我們可以使用接受 JavaType 的重載的 readValue 方法:

@Test
public void givenJsonArray_whenDeserializingAsListWithJavaTypeHelp_thenCorrect()
  throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper();

    List listOfDtos = Lists.newArrayList(
      new MyDto("a", 1, true), new MyDto("bc", 3, false));
    String jsonArray = mapper.writeValueAsString(listOfDtos);

    CollectionType javaType = mapper.getTypeFactory()
      .constructCollectionType(List.class, MyDto.class);
    List asList = mapper.readValue(jsonArray, javaType);

    assertThat(asList.get(0), instanceOf(MyDto.class));
}

最後,MyDto 類需要有無參數的默認構造函數——如果它沒有,Jackson 將無法實例化它

com.fasterxml.jackson.databind.JsonMappingException: 
No suitable constructor found for type [simple type, class org.baeldung.jackson.ignore.MyDto]: 
can not instantiate from JSON object (need to add/enable type information?]

或者,我們可以使用 @JsonCreator 註解來調整用於反序列化的構造函數。

4. 結論

將 JSON 數組映射到 Java 集合是 Jackson 最常用的任務之一,這些解決方案 對於實現正確、類型安全的映射至關重要

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.