简体   繁体   English

使用 Jackson Object Mapper 将整数列表从 json 反序列化为 java

[英]deserialise the list of integer from json to java using Jackson Object Mapper

I have my json response like below:我的 json 响应如下:

{"IsValid":false,"ModelErrors":null,"ValidationErrors":[10000]}

model class:模型类:

public class ShipmentResponse {
    private boolean isValid;
    private ModelErrors modelErrors;
    private List<Integer> validationErrors = null;

Object Mapper code :对象映射器代码:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ShipmentResponse shipmentResponse =  mapper.readValue((BufferedInputStream)response.getEntity(), ShipmentResponse.class);

i could not able to map the validationErrors from json to java ,ie, validationErrors = null after parsing .Im expecting validationErrors = {1000} but not sure why?我无法将validationErrors从json 映射到java,即validationErrors = null 解析后。我期待validationErrors = {1000} 但不知道为什么? i know we can use TypeReference to return array or list but not nested inside data object.我知道我们可以使用 TypeReference 来返回数组或列表,但不能嵌套在数据对象中。

Try this尝试这个

    public class ShipmentResponse {

        @JsonProperty("IsValid")
        private boolean isValid;
        @JsonProperty("ModelErrors")
        private ModelErrors modelErrors;
        @JsonProperty("ValidationErrors")
        private List<Integer> validationErrors = null;
}

In general you have missmatch in your property names and actual json (case matters)通常,您的属性名称和实际 json 不匹配(大小写很重要)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM