Cannot deserialize instance of `org.json.JSONObject`

Nuñito de la Calzada :

I have a basic SpringBoot 2.1.5.RELEASE app. Using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file with some RestControllers.

In 1 of the controller this is the body I send:

{
    "depositHotel": "xxx",
    "destinationHotel": "aaa",
    "depositHotelAmount": "0.2",
    "destinationHotelAmount": "4",
    "destinationAddress": [{
        "address": "asdf",
        "tag": ""
    }],
    "refundAddress": [{
        "address": "pio",
        "tag": ""
    }]
}

so I create this class to use it as a RequestBody:

public class HotelswitchHotelOrderRequestBody {


    public static class Builder {

        private String depositHotel;
        private String destinationHotel;
        private Float depositHotelAmount;
        private Float destinationHotelAmount;
        private JSONObject destinationAddress;
        private JSONObject refundAddress;


        public Builder(String depositHotel, String destinationHotel) {
            this.depositHotel = depositHotel;
            this.destinationHotel = destinationHotel;
        }

        public Builder withDepositHotelAmount (Float depositHotelAmount) {
            this.depositHotelAmount = depositHotelAmount;
            return this;  
        }

        public Builder withDestinationHotelAmount (Float destinationHotelAmount) {
            this.destinationHotelAmount = destinationHotelAmount;
            return this;  
        }

        public Builder toDestinationAddress (JSONObject destinationAddress) {
            this.destinationAddress = destinationAddress;
            return this;  
        }

        public Builder toRefundAddress (JSONObject refundAddress) {
            this.refundAddress = refundAddress;
            return this;  
        }

        public HotelswitchHotelOrderRequestBody build(){

            HotelswitchHotelOrderRequestBody order = new HotelswitchHotelOrderRequestBody(); 
            order.depositHotel = this.depositHotel;
            order.depositHotelAmount = this.depositHotelAmount;
            order.destinationAddress = this.destinationAddress;
            order.destinationHotel = this.destinationHotel;
            order.destinationHotelAmount = this.destinationHotelAmount;
            order.refundAddress = this.refundAddress;

            return order;

        }
    }


    private String depositHotel;
    private String destinationHotel;
    private Float depositHotelAmount;
    private Float destinationHotelAmount;
    private JSONObject destinationAddress;
    private JSONObject refundAddress;


    private HotelswitchHotelOrderRequestBody () {
        //Constructor is now private.
    }


    public String getDepositHotel() {
        return depositHotel;
    }


    public void setDepositHotel(String depositHotel) {
        this.depositHotel = depositHotel;
    }


    public String getDestinationHotel() {
        return destinationHotel;
    }


    public void setDestinationHotel(String destinationHotel) {
        this.destinationHotel = destinationHotel;
    }


    public Float getDepositHotelAmount() {
        return depositHotelAmount;
    }


    public void setDepositHotelAmount(Float depositHotelAmount) {
        this.depositHotelAmount = depositHotelAmount;
    }


    public Float getDestinationHotelAmount() {
        return destinationHotelAmount;
    }


    public void setDestinationHotelAmount(Float destinationHotelAmount) {
        this.destinationHotelAmount = destinationHotelAmount;
    }


    public JSONObject getDestinationAddress() {
        return destinationAddress;
    }


    public void setDestinationAddress(JSONObject destinationAddress) {
        this.destinationAddress = destinationAddress;
    }


    public JSONObject getRefundAddress() {
        return refundAddress;
    }


    public void setRefundAddress(JSONObject refundAddress) {
        this.refundAddress = refundAddress;
    }




}

But I have this error when receiving the object:

JSON parse error:  out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `org.json.JSONObject` out of START_ARRAY token
Nestor Sokil :

JSONObject's representation in actual JSON is a hash i.e. {...}. In your json data you're providing an array of hashes [{...}] which is not the same. Judging from your domain I don't think it has to be multiple values, so you can just omit [] in your payload and if it does then the fields in your Java class can be defined as JSONArray.

However, I think you should go with defining an Address class and either using

private Address destinationAddress;
private Address refundAddress;

or if it indeed does have to be an object array

private List<Address> destinationAddresses;
private List<Address> refundAddresses;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=146332&siteId=1