簡體   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