使用 Jackson Objectmapper 反序列化从 json 到 java 的整数列表

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

我的 json 回复如下:

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

型号class:

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

对象映射器代码:

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

我无法将 validationErrors 从 json 映射到 java,即解析后 validationErrors = null。我期待 validationErrors = {1000但不确定为什么?我知道我们可以对 return 数组或列表使用 TypeReference,但不能嵌套在数据对象中。

试试这个

    public class ShipmentResponse {

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

一般来说,您的 属性 姓名和实际 json 姓名不匹配(大小写很重要)