简体   繁体   中英

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

i need to serialize a jSon String with jackson to an Object.

The string i get is

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

My object is

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

    private String count;

    private int status;

    private String registration_error;

    private int usable;

// getters and setters

So, this is how i'm trying to get my Wrapper

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

But when i log it i get

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

What's wrong with it?

Thanks

You are usin @jsonRootName annotation which is correct. However, the annotation does not work on its own. You have to configure DeserializationFeature for object mapper. Your code should look like:

    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);

Check wrapper , It is having count and status as given in the json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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