繁体   English   中英

Spring Jackson-无法识别的字段“ response”在以下位置未标记为可忽略

[英]Spring Jackson - Unrecognized field \“response\” not marked as ignorable at

我需要将带有杰克逊的jSon字符串序列化为对象。

我得到的字符串是

{"response":{"status":1,"count":"90120"}}

我的对象是

@JsonRootName("response")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Wrapper {

    private String count;

    private int status;

    private String registration_error;

    private int usable;

// getters and setters

因此,这就是我要获取包装纸的方式

String response = {"response":{"status":1,"count":"90120"}};
        ObjectMapper mapper = new ObjectMapper();
        Wrapper w = mapper.readValue(response, Wrapper.class);

但是当我登录时我得到

Wrapper [count=null, status=0, registration_error=null, usable=0]

它出什么问题了?

谢谢

您使用的是@jsonRootName注释,这是正确的。 但是,注释不能单独工作。 您必须为对象映射器配置DeserializationFeature。 您的代码应如下所示:

    String response="{\"response\":{\"status\":1,\"count\":\"90120\"}}";
            ObjectMapper mapper = new ObjectMapper();

            mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

 //mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true); This is for serailization time
            Wrapper wrapper = mapper.readValue(response, Wrapper.class);

检查包装器,它具有json中给出的计数和状态。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM