Since you get the NearbyPlace s collection, it might be best for you to just navigate the JsonNode screen. Otherwise, you're talking about redefining deserialization for collections or writing a deserializer that can become annoying.
The example below is recursive. Recursion in Java is bad (at the moment), but interesting to write. In a working application, I recommend a loop.
@Test public void testNearbyPlaceDeserialization() throws Exception { JsonNode jsonNode = objectMapper.readTree(new File("input.json")); // or objectMapper.readValue(resultString, JsonNode.class); ImmutableList<NearbyPlace> nearbyPlaces = readLatLng(jsonNode, jsonNode.get("vicinity").asText(null), ImmutableList.builder()); System.out.println(nearbyPlaces); } private static ImmutableList<NearbyPlace> readLatLng(JsonNode jsonNode, String vicinity, ImmutableList.Builder<NearbyPlace> placeBuilder) { JsonNode latNode = jsonNode.get("lat"); JsonNode lngNode = jsonNode.get("lng"); if (latNode != null && lngNode != null) { placeBuilder.add(NearbyPlace.builder() .setLatitude(latNode.asDouble()) .setLongitude(lngNode.asDouble()) .setVicinity(vicinity) .build()); } else { jsonNode.elements().forEachRemaining((element) -> { readLatLng(element, vicinity, placeBuilder); }); } return placeBuilder.build(); }
This will return a list of 3 NearbyPlace s.
source share