So, I've looked around for this answer but I haven't found a solution that mirrors my situation. Basically, I have a JSON structure that looks like:
{
"count": 4424,
"results": [
{"id": 2718, "name": "fox", "location": "Omaha"},
{"id": 2719, "name": "bear", "location": "Miami"}
...
more objects containing an ID field
...
]
}
I want to be able to parse out all of the "id" values and store them in a list which I will loop over to make subsequent REST calls to other end points. If I wanted the entire object containing the id field, I know I could create a new object and create a custom JSON deserializer, but since it is just one field I want, that feels like overkill.
My initial thought was to do something in a custom deserializer, like so:
List<String> idList = new ArrayList<String>();
JsonNode node = jp.getCodec().readTree(jp); // jp = JsonParser
JsonNode resultsNode = node.get("results");
resultsNode.forEach(result -> {
JsonNode idNode = result.get("id");
if (idNode != null) {
String id = idNode.textValue().toUpperCase();
idList.add(id);
}
});
Would that be the correct way to handle this? If not, what would be the best/efficient way to parse these id values into a list?
Also, I was reading online about maybe wanting to do caching when attempting something like this (list may contain 1000+ IDs), but I'm not quite sure what that means in this scenario. Would anyone be able to explain that?