I have a class POJO
Class Pojo {
String id;
String name;
//getter and setter
}
I have a json like
{
\"response\" : [
Pojo pojo;
json = {
"response" : [
{
"id" : "1a",
"name" : "foo"
},
{
"id" : "1b",
"name" : "bar"
}
]
}
ObjectMapper mapper = new ObjectMapper();
JsonNode root = objectMapper.readTree(json);
pojo = objectMapper.readValue(root.path("response").toString(),new TypeReference>() {});
First, you have to create a JSON node with your JSON file. Now you have a JSON node. You can go to the desired location using path function of JSON node like what I did
root.path("response")
However this will return a JSON tree. To make a String, I have used the toString method. Now, you have a String like below " [ { "id" : "1a", "name" : "foo" }, { "id" : "1b", "name" : "bar" } ] " You can map this String with JSON array as following
String desiredString = root.path("response").toString();
pojos = objectMapper.readValue(desiredString ,new TypeReference>() {});