Sasi Kathimanda
Sasi Kathimanda

Reputation: 1806

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

I have my json response like below:

{"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 ,i.e., validationErrors = null after parsing .Im expecting validationErrors = {1000} but not sure why? i know we can use TypeReference to return array or list but not nested inside data object.

Upvotes: 1

Views: 1533

Answers (1)

Antoniossss
Antoniossss

Reputation: 32527

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)

Upvotes: 2

Related Questions