为什么Jackson Object Mapper无法反序列化这个嵌套的JSON?

时间:2018-01-25 16:15:02

标签: java json jackson

我正在更新一些代码以获得新API,该API返回相同类型的数据,但采用不同的JSON格式。这是请求返回的示例:

{
    "code": 200,
    "message": "OK",
    "data": [
        {
            "start_time": "2017-09-20T00:00:00.000-04:00",
            "end_time": "2017-09-21T00:00:00.000-04:00",
            "value": 8612.637512577203
        },
        {
            "start_time": "2017-09-21T00:00:00.000-04:00",
            "end_time": "2017-09-22T00:00:00.000-04:00",
            "value": 8597.89155775999
        },
        {
            "start_time": "2017-09-22T00:00:00.000-04:00",
            "end_time": "2017-09-23T00:00:00.000-04:00",
            "value": 24584.603303989123
        }
    ],
    "meta": {
        "space_id": "e1c38410-f912-4ae3-9db9-10a1ad1e3bf5",
        "channel_id": 1,
        "aggregation_type": "period_beginning",
        "pids_count": 1,
        "timezone": "America/New_York"
    }
}

我想忽略代码和消息属性,并将数据数组放入地图中的列表中,其中键是&#34; space_id&#34; property (Map<String, List<Reading>>),用于与旧实现兼容。这是我创建的包含反序列化对象的POJO:

@JsonIgnoreProperties({"code", "message", "meta"})
public class GetReadingsResult {

    @JsonIgnore
    private Map<String, List<Reading>> readings;

    @JsonIgnore
    public GetReadingsResult(Map<String, List<Reading>> readings) {
        this.readings = readings;
    }

    @JsonCreator
    public GetReadingsResult(@JsonProperty("data")Reading[] data, @JsonProperty("space_id")String spaceId) {
        this.readings.put(spaceId, Arrays.asList(data));
    }

    //...other getters and setters...
}

在我的测试中,我在测试JSON文件上调用readValue并收到以下错误:

com.fasterxml.jackson.databind.JsonMappingException: Instantiation of [simple type, class model.GetReadingsResult] value failed: null

设置/注释我的POJO以处理这个嵌套文件的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

这是正确的实施方式。

请注意所有事物的对象观点:

这些是pojos:

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "start_time",
    "end_time",
    "value"
})
public class Datum {

    @JsonProperty("start_time")
    public String startTime;
    @JsonProperty("end_time")
    public String endTime;
    @JsonProperty("value")
    public Double value;

}


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "space_id",
    "channel_id",
    "aggregation_type",
    "pids_count",
    "timezone"
})
public class Meta {

    @JsonProperty("space_id")
    public String spaceId;
    @JsonProperty("channel_id")
    public Integer channelId;
    @JsonProperty("aggregation_type")
    public String aggregationType;
    @JsonProperty("pids_count")
    public Integer pidsCount;
    @JsonProperty("timezone")
    public String timezone;

}

这是包装器根对象:

import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "code",
    "message",
    "data",
    "meta"
})
public class ExampleStack {

    @JsonProperty("code")
    public Integer code;
    @JsonProperty("message")
    public String message;
    @JsonProperty("data")
    public List<Datum> data = null;
    @JsonProperty("meta")
    public Meta meta;

}

这是工作的例子:

import com.fasterxml.jackson.databind.ObjectMapper;//<--IMPORTANT!

public class TestJacksonObject {


     public static void main(String[] args) {


         ObjectMapper mapper =  new ObjectMapper(); 
         ExampleStack stack = null;
            try {
                stack = mapper .readValue( new FileInputStream(new File("C://test.json")) , ExampleStack.class);

            } catch (IOException e) {
                e.printStackTrace();
            }

    System.out.println(stack.message);//<--Do whatever you want...
.....

如果制作所有这些课程太难了,这实际上很乏味,我建议你通过这个有用的网站在线自动生成它们:

Json2Pojo

希望它可以帮助你!