Simple Jackson deserialization of nested objects

為{幸葍}努か 提交于 2021-01-28 04:40:22

问题


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

I have the following JSON response

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

I've tried converting it with simple POJO's

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

    String id;

    List<Car> cars;

}

And

public class Car {

    String id;

}

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?


回答1:


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);
    }

}

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());
        }

    }

}

Hope this helps.



来源:https://stackoverflow.com/questions/50072929/simple-jackson-deserialization-of-nested-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!