我正在尝试将Json转换为Java对象.我有一个名为’result’的String,我想将其转换为类对象为TransferRecord.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);
问题是当我尝试创建对象时,即使String中有相应的数据,所有三种方法中的每个值都设置为null.有人可以指出我在这里做错了什么吗?
提前致谢.