Convert json to Java Object - Properties set to Null

Supun Amarasinghe :

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. :)

Bor Laze :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=90983&siteId=1