thisisjingjing
thisisjingjing

Reputation: 13

How to convert part of the jackson file with ObjectMapper

I have a json file like this:

[{name:object1},{name:object2}]

And I can read it by:

mapper.readValue(myJsonFile, MyObject.class);

However, now the file is changed to be:

{"status":true, "result":[{name:object1},{name:object2}]}

Now how to read the MyObject?

Upvotes: 0

Views: 182

Answers (1)

Mohit
Mohit

Reputation: 1755

First get the JSONNode of result and then use that node as root to parse.

public static void main(String[] args) {

    ObjectMapper om = new ObjectMapper();
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("wrapper.json");
    try {
        JsonNode node = om.readValue(in, JsonNode.class).get("result");
        JsonParser parser = node.traverse();
        List l = om.readValue(parser, List.class); //Read to MyObject instead of List.
        System.out.println(l);
    } catch (Exception e){
        e.printStackTrace();
    }
}

Upvotes: 1

Related Questions