简体   繁体   中英

Convert json to Java Object - Properties set to Null

I am trying to convert a Json to a Java object.I have a String named ' result ' and I want to Convert it into a Java Object whose class is TransferRecord.java

This is a part of the string I use as the input.

{
  "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"
    }
  ]
}

This is the TransferRecord class. I checked the json mapping and they are perfectly identical.Note that there are more fields available in class but I just pasted a part of it.The number of properties in input string and the java class are same.

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;
  }
}

Following is my code which I used for the conversion.Note that these three pieces of code would serve the same purpose and I tried them separately.

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);

Problem is when I try to create the object,every value is set to null in all three approaches even though there are corresponding data available in the String. Can someone please point out what I am doing wrong here?

Thanks in advance. :)

first, json should be :

{
    "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"
  }

second, maybe you should add getter and setter methods for each field。

Use toString methods to print values directly and structure your classes as follows

ObjectMapper mapper = new ObjectMapper();

        Data data = mapper.readValue(string, Data.class);

        System.out.println(data);

Data class

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
                + "]";
    }
}

Error Code Class:

public class ErrorCode {
    @JsonProperty("Code")
    private String code;

    @JsonProperty("Context")
    private String context;

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

TransferRecord Class:

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 Class:

public class TransferId {
    @JsonProperty("TransferRef")
    private String transferRef;

    @JsonProperty("DistributorRef")
    private String distributorRef;

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

Price Class:

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 + "]";
    }
}

This it NOT TransferRecord class. Your json fetch class with 3 fields:

public class Something {
    @JsonProperty("TransferRecord")
    private TransferRecord transferRecord;
    @JsonProperty("ResultCode")
    private int resultCode;
    @JsonProperty("ErrorCodes")
    private List<ErrorCode> errorCodes;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM