代码之家  ›  专栏  ›  技术社区  ›  Supun Amarasinghe

将JSON转换为Java对象-属性设置为NULL

  •  3
  • Supun Amarasinghe  · 技术社区  · 6 年前

    我试图把JSON转换成Java对象。 结果 我想把它转换成一个Java对象,它的类是 传输记录.java

    这是我用作输入的字符串的一部分。

    {
      "TransferRecord": {
        "TransferId": {
          "TransferRef": "string",
          "DistributorRef": "string"
        },
        "SkuCode": "string",
        "Price": {
          "CustomerFee": 0,
          "DistributorFee": 0,
          "ReceiveValue": 0,
          "ReceiveCurrencyIso": "string",
          "ReceiveValueExcludingTax": 0,
          "TaxRate": 0,
          "TaxName": "string",
          "TaxCalculation": "string",
          "SendValue": 0,
          "SendCurrencyIso": "string"
        },
        "CommissionApplied": 0,
        "StartedUtc": "2019-01-31T10:10:20.527Z",
        "CompletedUtc": "2019-01-31T10:10:20.527Z",
        "ProcessingState": "string",
        "ReceiptText": "string",
        "ReceiptParams": {},
        "AccountNumber": "string"
      },
      "ResultCode": 0,
      "ErrorCodes": [
        {
          "Code": "string",
          "Context": "string"
        }
      ]
    }
    

    这是TransferRecord类。我检查了JSON映射,它们完全相同。注意,在类中有更多字段可用,但我只是粘贴了一部分。输入字符串和Java类中的属性数是相同的。

    public class TransferRecord   {
      @JsonProperty("TransferId")
      private TransferId transferId = null;
    
      @JsonProperty("SkuCode")
      private String skuCode = null;
    
      @JsonProperty("Price")
      private Price price = null;
    
      @JsonProperty("CommissionApplied")
      private BigDecimal commissionApplied = null;
    
      @JsonProperty("StartedUtc")
      private Date startedUtc = null;
    
      @JsonProperty("CompletedUtc")
      private Date completedUtc = null;
    
      @JsonProperty("ProcessingState")
      private String processingState = null;
    
      @JsonProperty("ReceiptText")
      private String receiptText = null;
    
      @JsonProperty("ReceiptParams")
      private Map<String, String> receiptParams = null;
    
      @JsonProperty("AccountNumber")
      private String accountNumber = null;
    
      public TransferRecord transferId(TransferId transferId) {
        this.transferId = transferId;
        return this;
      }
    }
    

    下面是我用于转换的代码。请注意,这三段代码的用途相同,我分别尝试了它们。

    ObjectMapper mapper = new ObjectMapper();
    
    //1 TransferRecord objTransRecord = mapper.readValue(result, TransferRecord.class);
    
    //2 TransferRecord objTransRecord = mapper.readerWithView(TransferRecord.class).forType(TransferRecord.class).readValue(result);
    
    //3 TransferRecord objTransRecord = mapper.readerFor(TransferRecord.class).readValue(result);
    

    问题是当我尝试创建对象时,每个值都设置为 无效的 在这三种方法中,即使字符串中有相应的可用数据。有人能指出我这里做错了什么吗?

    事先谢谢。:)

    3 回复  |  直到 6 年前
        1
  •  1
  •   Bor Laze    6 年前

    它不传输记录类。您的json fetch类有3个字段:

    public class Something {
        @JsonProperty("TransferRecord")
        private TransferRecord transferRecord;
        @JsonProperty("ResultCode")
        private int resultCode;
        @JsonProperty("ErrorCodes")
        private List<ErrorCode> errorCodes;
    }
    
        2
  •  2
  •   denny    6 年前

    首先,JSON应该是:

    {
        "TransferId": {
          "TransferRef": "string",
          "DistributorRef": "string"
        },
        "SkuCode": "string",
        "Price": {
          "CustomerFee": 0,
          "DistributorFee": 0,
          "ReceiveValue": 0,
          "ReceiveCurrencyIso": "string",
          "ReceiveValueExcludingTax": 0,
          "TaxRate": 0,
          "TaxName": "string",
          "TaxCalculation": "string",
          "SendValue": 0,
          "SendCurrencyIso": "string"
        },
        "CommissionApplied": 0,
        "StartedUtc": "2019-01-31T10:10:20.527Z",
        "CompletedUtc": "2019-01-31T10:10:20.527Z",
        "ProcessingState": "string",
        "ReceiptText": "string",
        "ReceiptParams": {},
        "AccountNumber": "string"
      }
    

    第二,也许你应该为每个字段添加getter和setter方法。

        3
  •  2
  •   Dinesh Shekhawat    6 年前

    使用ToString方法直接打印值并按如下方式构造类

    ObjectMapper mapper = new ObjectMapper();
    
            Data data = mapper.readValue(string, Data.class);
    
            System.out.println(data);
    

    数据类

    public class Data {
        @JsonProperty("TransferRecord")
        private TransferRecord transferRecord;
    
        @JsonProperty("ResultCode")
        private int
        resultCode;
    
        @JsonProperty("ErrorCodes")
        private List<ErrorCode> errorCodes;
    
        @Override
        public String toString() {
            return "Data [transferRecord=" + transferRecord + ", resultCode=" + resultCode + ", errorCodes=" + errorCodes
                    + "]";
        }
    }
    

    错误代码类:

    public class ErrorCode {
        @JsonProperty("Code")
        private String code;
    
        @JsonProperty("Context")
        private String context;
    
        @Override
        public String toString() {
            return "ErrorCode [code=" + code + ", context=" + context + "]";
        }
    }
    

    TransferRecord类:

    public class TransferRecord {
        @JsonProperty("TransferId")
        private TransferId transferId;
    
        @JsonProperty("SkuCode")
        private String skuCode;
    
        @JsonProperty("Price")
        private Price price;
    
        @JsonProperty("CommissionApplied")
        private BigDecimal commissionApplied;
    
        @JsonProperty("StartedUtc")
        private Date startedUtc;
    
        @JsonProperty("CompletedUtc")
        private Date completedUtc;
    
        @JsonProperty("ProcessingState")
        private String processingState;
    
        @JsonProperty("ReceiptText")
        private String receiptText;
    
        @JsonProperty("ReceiptParams")
        private Map<String, String> receiptParams;
    
        @JsonProperty("AccountNumber")
        private String accountNumber;
    
        @Override
        public String toString() {
            return "TransferRecord [transferId=" + transferId + ", skuCode=" + skuCode + ", price=" + price
                    + ", commissionApplied=" + commissionApplied + ", startedUtc=" + startedUtc + ", completedUtc="
                    + completedUtc + ", processingState=" + processingState + ", receiptText=" + receiptText
                    + ", receiptParams=" + receiptParams + ", accountNumber=" + accountNumber + "]";
        }
    }
    

    TransferID类:

    public class TransferId {
        @JsonProperty("TransferRef")
        private String transferRef;
    
        @JsonProperty("DistributorRef")
        private String distributorRef;
    
        @Override
        public String toString() {
            return "TransferId [transferRef=" + transferRef + ", distributorRef=" + distributorRef + "]";
        }
    }
    

    价格等级:

    public class Price {
        @JsonProperty("CustomerFee")
        private int customerFee;
    
        @JsonProperty("DistributorFee")
        private int distributorFee;
    
        @JsonProperty("ReceiveValue")
        private int receiveValue;
    
        @JsonProperty("ReceiveCurrencyIso")
        private String receiveCurrencyIso;
    
        @JsonProperty("ReceiveValueExcludingTax")
        private int receiveValueExcludingTax;
    
        @JsonProperty("TaxRate")
        private int taxRate;
    
        @JsonProperty("TaxName")
        private String taxName;
    
        @JsonProperty("TaxCalculation")
        private String taxCalculation;
    
        @JsonProperty("SendValue")
        private int sendValue;
    
        @JsonProperty("SendCurrencyIso")
        private String sendCurrencyIso;
    
        @Override
        public String toString() {
            return "Price [customerFee=" + customerFee + ", distributorFee=" + distributorFee + ", receiveValue="
                    + receiveValue + ", receiveCurrencyIso=" + receiveCurrencyIso + ", receiveValueExcludingTax="
                    + receiveValueExcludingTax + ", taxRate=" + taxRate + ", taxName=" + taxName + ", taxCalculation="
                    + taxCalculation + ", sendValue=" + sendValue + ", sendCurrencyIso=" + sendCurrencyIso + "]";
        }
    }
    
    推荐文章