我从 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"**
我试图找到一个正当的理由,并理解杰克逊无法为同一财产找到合适的二传手。 我不确定如何处理这种情况。有什么想法吗?
添加另一个字段作为分子类型,并为其添加 setter getter 方法并尝试。
private BigDecimal numeratorType;
public BigDecimal setNumeratorType(BigDecimal numeratorType) {
this.numeratorType = numeratorType;
}
public BigDecimal getNumeratorType() {
return this.numeratorType
}
更新了答案
它应该如下:
DataResponse user = mapper.readValue(test.toString(), new TypeReference<DataResponse>(){});
如果您有一个JSONs
列表,则可能如下所示:
DataResponse user = mapper.readValue(test.toString(), new TypeReference<List<DataResponse>>(){});