jackson - read specific field from string list of jsons

Viewed 1707

I'm pretty new to world of jackson, and wanted to read the value of specific field from list of jsons (which is a response body of third-party api).

for a single json, using objectMapper works fine.

        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(sampleString);
        JsonNode idNode = rootNode.path("id");
        System.out.println("id: "+ idNode.asText());

But I want to parse list of jsons (which is a string coming from a response body) .So for example I receive this body:

[  
{  
  "id":10,
  "firstName":"Jack",
  "primaryPhone":"9999999999",
  "email":"jack@me.com"
},
{  
  "id":4,
  "firstName":"Mark",
  "primaryPhone":"9999999991",
  "email":"mark@me.com"
},
{  
  "id":12,
  "firstName":"Susaan",
  "primaryPhone":"9999999992",
  "email":"susan23@me.com"
}
]

I want to read the ids first, and if I find a specific id, return some other info from that block.

For example if id=4, read the firstName and email of that person.

But I'm not sure how to parsee list of json.

Any suggestions/comments is appreciated.

3 Answers
Related