嵌套对象的简单Jackson反序列化 [英] Simple Jackson deserialization of nested objects

查看:327
本文介绍了嵌套对象的简单Jackson反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是微不足道的,但是由于某种原因,我似乎无法正确地做到这一点.

This should be trivial but for some reason I can't seem to get it right.

我有以下JSON响应

{
  "info": "processing",
  "data": {
    "id": "123",
    "cars": [
      {
        "id": "1"
      },
      {
        "id": "2"
      }
    ]
  }
}

我尝试用简单的POJO进行转换

I've tried converting it with simple POJO's

@JsonRootName(value = "data")
public class Product {

    String id;

    List<Car> cars;

}

还有

public class Car {

    String id;

}

但是会返回一个空的Product对象,其id和product为null.当然,我不需要为此简单操作编写自定义JsonDeserialize吗?

But that returns an empty Product object with the id and products null. Surely I don't need to write a custom JsonDeserialize for this simple action?

推荐答案

您必须创建POJO,然后使用Jackson ObjectMapper API读取Java对象的JSON字符串.

You have to create the POJO and then use the Jackson ObjectMapper API to read the JSON string to java object.

这是基于您的示例字符串的工作代码.

Here is the working code basing on your sample string.

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "info", "data" })
public class Process {

    @JsonProperty("info")
    private String info;
    @JsonProperty("data")
    private Data data;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("info")
    public String getInfo() {
        return info;
    }

    @JsonProperty("info")
    public void setInfo(String info) {
        this.info = info;
    }

    @JsonProperty("data")
    public Data getData() {
        return data;
    }

    @JsonProperty("data")
    public void setData(Data data) {
        this.data = data;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "id", "cars" })
public class Data {

    @JsonProperty("id")
    private String id;
    @JsonProperty("cars")
    private List<Car> cars = null;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("id")
    public String getId() {
        return id;
    }

    @JsonProperty("id")
    public void setId(String id) {
        this.id = id;
    }

    @JsonProperty("cars")
    public List<Car> getCars() {
        return cars;
    }

    @JsonProperty("cars")
    public void setCars(List<Car> cars) {
        this.cars = cars;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}


@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "id" })
public class Car {

    @JsonProperty("id")
    private String id;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("id")
    public String getId() {
        return id;
    }

    @JsonProperty("id")
    public void setId(String id) {
        this.id = id;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

反序列化JSON字符串的代码.

Code to deserialize the JSON string.

import java.io.IOException;
import java.util.List;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class MainApp {

    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

        String input = "{\r\n" + 
                "  \"info\": \"processing\",\r\n" + 
                "  \"data\": {\r\n" + 
                "    \"id\": \"123\",\r\n" + 
                "    \"cars\": [\r\n" + 
                "      {\r\n" + 
                "        \"id\": \"1\"\r\n" + 
                "      },\r\n" + 
                "      {\r\n" + 
                "        \"id\": \"2\"\r\n" + 
                "      }\r\n" + 
                "    ]\r\n" + 
                "  }\r\n" + 
                "}";
        ObjectMapper mapper = new ObjectMapper();
        Process process = mapper.readValue(input, Process.class);
        System.out.println(process.getInfo());
        Data data = process.getData();
        List<Car> cars = data.getCars();
        for(Car car : cars) {
            System.out.println(car.getId());
        }

    }

}

希望这会有所帮助.

这篇关于嵌套对象的简单Jackson反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆