2

I've got a JSON like this:

    {
        "result": [
            {
                "reservation_id": 101,
                "euro_fee": 11.00,
                "hotel_id": 1
            },
            {
                "reservation_id": 102,
                "euro_fee": 12.00,
                "hotel_id": 2
            },
            {
                "reservation_id": 103,
                "euro_fee": 13.00,
                "hotel_id": 3
            }
        ],
        "meta": {
            "ruid": "0001="
        }
    }

and I'm trying to use Jackson (with Spring Boot) for parse and bind it. Here is my POJO's:

Response.java

    public class Response {

        private Result result;
        private Meta meta;

        public Response() {
        }

        public Result getResult() {
            return result;
        }

        public void setResult(Result result) {
            this.result = result;
        }

        public Meta getMeta() {
            return meta;
        }

        public void setMeta(Meta meta) {
            this.meta = meta;
        }
    }

Meta.java

    public class Meta {

        private String ruid;

        public Meta() {
        }

        public String getRuid() {
            return ruid;
        }

        public void setRuid(String ruid) {
            this.ruid = ruid;
        }
    }

Result.java

    public class Result {

        private Booking[] results;

        public Result() {
        }

        public Booking[] getResults() {
            return results;
        }

        public void setResult(Booking[] results) {
            this.results = results;
        }
    }

Booking.java

    public class Booking {


        private long reservation_id;

        private long hotel_id;

        private Double euro_fee;

        public Booking() {
        }

        public long getReservation_id() {
            return reservation_id;
        }

        public void setReservation_id(long reservation_id) {
            this.reservation_id = reservation_id;
        }

        public long getHotel_id() {
            return hotel_id;
        }

        public void setHotel_id(long hotel_id) {
            this.hotel_id = hotel_id;
        }


        public Double getEuro_fee() {
            return euro_fee;
        }

        public void setEuro_fee(Double euro_fee) {
            this.euro_fee = euro_fee;
        }
    }

I can get ruid from meta using:

        // getting JSON from REST
        String response = restTemplate.postForObject(resourceURL, httpEntity, String.class);

        // use jackson
        ObjectMapper mapper = new ObjectMapper();
        Response theResponse = mapper.readValue(response, Response.class);
        System.out.println("getRuid: " + theResponse.getMeta().getRuid());

but I can't get objects or single item from nested array. When I'm trying to get array of items I'm getting error:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of out of START_ARRAY token at [Source: (String)...

I know this one should be easy, but I'm using Jackson for the first time and the problem might be somewhere in the deep.

1 Answer 1

2

Change your Response class.

Try with this:

public class Response {

        private List<Booking> result;
        private Meta meta;
//getter setter
}
Sign up to request clarification or add additional context in comments.

1 Comment

This helps me + I have excluded Result.java

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.