Jackson在将JSON序列化为POJO

时间:2020-01-02 11:30:03

标签: java json jackson

我从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\"**

我试图找到一个有效的理由,并理解杰克逊无法以某种方式为相同的财产找到合适的二传手。 我不确定如何处理这种情况。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

更新后的答案

应如下所示:

DataResponse user = mapper.readValue(test.toString(), new TypeReference<DataResponse>(){});

如果您有JSONs的列表,则可能如下:

DataResponse user = mapper.readValue(test.toString(), new TypeReference<List<DataResponse>>(){});

答案 1 :(得分:1)

添加另一个字段作为numeratorType并为其添加setter getter方法,然后尝试。

    private BigDecimal numeratorType;

    public BigDecimal setNumeratorType(BigDecimal numeratorType) {
         this.numeratorType = numeratorType;

    }

    public BigDecimal getNumeratorType() {
        return this.numeratorType 

    }