使用Jackson将反序列化JSON数组反序列化为单个Java对象

基里尔:

我的想法是,我想将JSON数组["foo", "bar"]转换为Java对象,因此需要按索引将每个数组元素映射到属性。

假设我有以下JSON:

{
  "persons": [
    [
      "John",
      "Doe"
    ],
    [
      "Jane",
      "Doe"
    ]
  ]
}

如您所见,每个人都只是一个数组,其中名字是索引为0的元素,姓氏是索引为1的元素。

我想反序列化为List<Person>我使用mapper如下:

mapper.getTypeFactory().constructCollectionType(List.class, Person.class)

在哪里Person.class

public class Person {
    public final String firstName;
    public final String lastName;

    @JsonCreator
    public Person(@JsonProperty() String firstName, @JsonProperty String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

我想知道是否可以以某种方式将数组索引指定为@JsonProperty参数而不是键名?

基里尔:

感谢bureaquete建议使用自定义解串器。但它更适合我注册SimpleModule而不是@JsonDeserialize注释。下面是完整的JUnit测试示例:

@RunWith(JUnit4.class)
public class MapArrayToObjectTest {
    private static ObjectMapper mapper;

    @BeforeClass
    public static void setUp() {
        mapper = new ObjectMapper();
        SimpleModule customModule = new SimpleModule("ExampleModule", new Version(0, 1, 0, null));
        customModule.addDeserializer(Person.class, new PersonDeserializer());
        mapper.registerModule(customModule);
    }

    @Test
    public void wrapperDeserializationTest() throws IOException {
        //language=JSON
        final String inputJson = "{\"persons\": [[\"John\", \"Doe\"], [\"Jane\", \"Doe\"]]}";
        PersonsListWrapper deserializedList = mapper.readValue(inputJson, PersonsListWrapper.class);
        assertThat(deserializedList.persons.get(0).lastName, is(equalTo("Doe")));
        assertThat(deserializedList.persons.get(1).firstName, is(equalTo("Jane")));
    }

    @Test
    public void listDeserializationTest() throws IOException {
        //language=JSON
        final String inputJson = "[[\"John\", \"Doe\"], [\"Jane\", \"Doe\"]]";
        List<Person> deserializedList = mapper.readValue(inputJson, mapper.getTypeFactory().constructCollectionType(List.class, Person.class));
        assertThat(deserializedList.get(0).lastName, is(equalTo("Doe")));
        assertThat(deserializedList.get(1).firstName, is(equalTo("Jane")));
    }
}

class PersonsListWrapper {
    public List<Person> persons;
}

class Person {
    final String firstName;
    final String lastName;

    Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

class PersonDeserializer extends JsonDeserializer<Person> {
    @Override
    public Person deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        JsonNode node = jp.readValueAsTree();
        return new Person(node.get(0).getTextValue(), node.get(1).getTextValue());
    }
}

请注意,如果不需要包装器对象,则可以按如下所示[["John", "Doe"], ["Jane", "Doe"]]直接List<Person>使用映射器反序列化JSON数组

List<Person> deserializedList = mapper.readValue(inputJson, mapper.getTypeFactory().constructCollectionType(List.class, Person.class));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

尝试在Spring-boot中使用Jackson将JSON数组反序列化为Java String

如何使用Jackson将Java Enums序列化和反序列化为JSON对象

使用Jackson在反序列化JSON数组中反序列化JSON数组

如何使用Jackson来反序列化对象数组

使用Jackson使用重载方法将JSON反序列化为对象

使用Jackson将Java中的JSON反序列化

将JSON反序列化为现有对象(Java)

使用Jackson将JSON反序列化为ArrayList <POJO>

如何使用SubType将json反序列化为对象?

如何使用Java的Jackson库将嵌套的JSON数组反序列化为Map?

将Json数组反序列化为对象列表

Java Jackson将纯Json数组或Json对象反序列化为单个Java对象(POJO)

Jackson:将JSON数组从JSON对象反序列化为Java列表

使用PHP中的类型将嵌套对象序列化/反序列化为JSON

使用RestSharp将JSON反序列化为对象或数组

使用LocalDateTime字段将JSON反序列化为对象

使用Jackson将针对JAVA类的JSON序列化和反序列化

如何将多个对象序列化/反序列化为单个XML文件?

将JSON对象反序列化为数组

如何使用Jackson将反序列化的JSON反序列化为忽略键的对象?

将JSON对象反序列化为数组

如何通过使用Jackson将JSON数组反序列化为单个链接列表

将JSON反序列化为C#对象-不反序列化任何数据

如何将包含不同数据类型的JSON数组反序列化为单个对象

使用Jackson将反序列化JSON数组反序列化为Map

Jackson,将普通 JSON 数组反序列化为单个 Java 对象

无法将 JSON 响应反序列化为 Java 对象

Jackson 将对象数组反序列化为数组列表

将 Json 反序列化为对象