第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Json 到 Java 對(duì)象 - 由于字段相同而失敗

Json 到 Java 對(duì)象 - 由于字段相同而失敗

紅顏莎娜 2023-01-05 16:50:16
我正在嘗試將 json 轉(zhuǎn)換為 java 對(duì)象。由于 json 中有相同的字段,因此會(huì)拋出這樣的錯(cuò)誤。com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "amount": com.gateway.model.Order#setAmount(1 params) vs com.gateway.model.Order#setAmount(1 params)這是 json(與我的問(wèn)題相關(guān)的部分)   "order":{        "amount":1.000,      "chargeback":{           "amount":0,         "currency":"BHD"      },   }這是java類(lèi)的相關(guān)部分。public class Order{    private double amount;    Chargeback ChargebackObject;    // Getter Methods    public double getAmount()    {        return amount;    // Setter Methods    public void setAmount(double amount)    {        this.amount = amount;    }}class Chargeback{    private double amount;    private String currency;    // Getter Methods    @JsonIgnore    public double getAmount()    {        return amount;    }    @JsonInclude(Include.NON_NULL)    public String getCurrency()    {        return currency;    }    // Setter Methods     public void setAmount(double cb_amount)    {        this.amount = cb_amount;    }    public void setCurrency(String currency)    {        this.currency = currency;    }  }請(qǐng)注意,Chargeback 類(lèi)位于 Order.java 文件中。我已經(jīng)嘗試@JsonIgnore注釋并刪除類(lèi)amount中的,chargeback但仍然存在錯(cuò)誤。有人可以為此提出解決方案嗎?
查看完整描述

1 回答

?
瀟瀟雨雨

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊

我修改了你的代碼,試試下面的代碼?;旧?,我做了以下幾件事。

  1. Order 內(nèi)部沒(méi)有 Chargeback 對(duì)象的 getter 和 setter,所以添加了。

  2. 注釋掉 @JsonIgnore 注釋。

課程如下

class Chargeback {

      private double amount;

      private String currency;


      // Getter Methods

    //  @JsonIgnore

      public double getAmount() {

        return amount;

      }


    //  @JsonInclude(Include.NON_NULL)

      public String getCurrency() {

        return currency;

      }


      // Setter Methods


      public void setAmount(double cb_amount) {

        this.amount = cb_amount;

      }


      public void setCurrency(String currency) {

        this.currency = currency;

      }

    }


    public class Order {

      private double amount;

      Chargeback ChargebackObject;


      public double getAmount() {

        return amount;

      }


      public void setAmount(double amount) {

        this.amount = amount;

      }


      public Chargeback getChargebackObject() {

        return ChargebackObject;

      }


      public void setChargebackObject(Chargeback chargebackObject) {

        ChargebackObject = chargebackObject;

      }

    }

下面給出了用于測(cè)試生成 Json 的代碼。


public class Test1 {

  public static void main(String[] args) throws Exception {


    Chargeback chargeback = new Chargeback();

    chargeback.setAmount(1234.00);

    chargeback.setCurrency("BHD");

    Order order = new Order();

    order.setAmount(2345.00);

    order.setChargebackObject(chargeback);


    ObjectMapper mapper = new ObjectMapper();

    String toJson = null;

    try {

      toJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(order);

    } catch (IOException e) {

      e.printStackTrace();

    }

    System.out.println("Complete Json = " + toJson);


// From Json String to Java Object

ObjectMapper mapper1 = new ObjectMapper();

Order order1 = mapper.readValue(toJson, Order.class);

System.out.println("Order Object -> " + order1);

  }

}

下面給出生成json。


{

  "amount" : 2345.0,

  "chargebackObject" : {

    "amount" : 1234.0,

    "currency" : "BHD"

  }

}


查看完整回答
反對(duì) 回復(fù) 2023-01-05
  • 1 回答
  • 0 關(guān)注
  • 309 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)