【问题标题】:Cannot deserialize instance of `org.json.JSONObject`无法反序列化“org.json.JSONObject”的实例
【发布时间】:2019-05-27 20:33:35
【问题描述】:

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

在控制器的 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

【问题讨论】:

  • 用 JSONArray 代替 JSONObject
  • 您提供的json无效。
  • nuñito ostia,这两个地址实际上都是您的 json 中的数组,并且有些引号不正确,尽管它可能是在此处复制时出现的
  • 您说的是“我期望的正文”,但能否请您发送您要发送的实际正文?
  • 消息说明了一切。 yoru JSON 中的destinationAddress 和refundAddress 都是数组,而不是对象。

标签: java json


【解决方案1】:

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

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

private Address destinationAddress;
private Address refundAddress;

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

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

【讨论】:

    【解决方案2】:

    我有一个类似的用例,我无法将 json 定义为 POJO。使用 com.fasterxml.jackson.databind.JsonNode 而不是 JSONObject 有效。

    【讨论】:

      猜你喜欢
      • 2019-07-06
      • 2020-11-26
      • 2019-09-22
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      相关资源
      最近更新 更多