简单杰克逊反序列化嵌套对象

时间:2018-04-28 04:44:46

标签: java json jackson

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

我有以下JSON响应

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

我尝试用简单的POJO转换它

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

    String id;

    List<Car> cars;

}

并且

public class Car {

    String id;

}

但是返回一个空的Product对象,其id和products为null。当然,我不需要为这个简单的动作编写自定义JsonDeserialize

1 个答案:

答案 0 :(得分:1)

您必须创建POJO,然后使用Jackson ObjectMapper API将JSON字符串读取到java对象。

以下是基于示例字符串的工作代码。

@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字符串的代码。

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

    }

}

希望这有帮助。