[英]How the Order of setter method call of POJO is determined in jackson while De-serializing Json?
[英]Jackson throwing "msg":"Unrecognized field \"numeratorType\" while de serializing the JSON into POJO
我從 POJO 類創建了一個 JSON 對象,其中一個包含以下代碼
@JsonProperty("numeratorType")
private BigDecimal numerator;
我再次使用以下代碼將相同的 JSON 對象(字符串)轉換為 POJO
JSONObject test = new JSONObject(/**JSON String***/);
ObjectMapper mapper = new ObjectMapper();
DataResponse user = mapper.readValue(test.toString(), DataResponse.class); <--error is thrown in this line
但是傑克遜在這樣做時拋出了以下錯誤
**Jackson throwing "msg":"Unrecognized field \"numeratorType\"**
我試圖為同樣的事情找到一個有效的理由,並理解傑克遜不知何故無法為同一屬性找到合適的設置器。 我不確定如何處理這種情況。 有什么想法嗎?
更新答案
它應該如下:
DataResponse user = mapper.readValue(test.toString(), new TypeReference<DataResponse>(){});
如果您有一個JSONs
列表,則可能如下所示:
DataResponse user = mapper.readValue(test.toString(), new TypeReference<List<DataResponse>>(){});
添加另一個字段作為 numeratorType 並為其添加 setter getter 方法並嘗試。
private BigDecimal numeratorType;
public BigDecimal setNumeratorType(BigDecimal numeratorType) {
this.numeratorType = numeratorType;
}
public BigDecimal getNumeratorType() {
return this.numeratorType
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.