Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 506
Supun Amarasinghe Avatar asked May 13 '26 16:05

Supun Amarasinghe


1 Answers

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。

like image 78
denny Avatar answered May 16 '26 06:05

denny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!