java - 无法反序列化 `org.json.JSONObject` 的实例

标签 java json

我有一个基本的 SpringBoot 2.1.5.RELEASE 应用程序。使用 Spring Initializer、JPA、嵌入式 Tomcat、Thymeleaf 模板引擎,并与一些 RestController 一起打包为可执行 JAR 文件。

在 Controller 1 中,这是我发送的正文:

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

所以我创建这个类将其用作 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;
    }




}

但是我在接收对象时遇到此错误:

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

最佳答案

JSONObject 在实际 JSON 中的表示是一个哈希,即 {...}。在您的 json 数据中,您提供了一个不同的哈希数组 [{...}] 。从您的域来看,我认为它不必是多个值,因此您可以在有效负载中省略 [] ,如果是,那么您的 Java 类中的字段可以定义为 JSONArray

但是,我认为您应该定义一个 Address 类并使用

private Address destinationAddress;
private Address refundAddress;

或者如果它确实必须是一个对象数组

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

关于java - 无法反序列化 `org.json.JSONObject` 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56332175/

相关文章:

ios - 如何将整数值解析为 Json iOS?

javascript - Cheerio 从 href 中的 json 获取图像

JAVA StdDateSerializer 解析日期

java - 使用一个不完全是 JSON 的主体

java - Log4j2 RollingFileAppender 构建器方法返回类型错误

java - 卡夫卡流: Grouping by a key in Json log

java - 如何访问我只通过字符串名称知道的类的类字段?

java - JUnit 如何测试迭代器

java - 无论如何,我无法在 Hibernate 中批处理 MySQL INSERT 语句

javascript - 使用 jQuery Ajax 发送 'pure' JSON 而不是多种形式