I have a complex JSON that I am trying to parse using Jackson JSON. I am a little confused about how to get into the latLng object to pull out the lat,lng values. This is part of the JSON:
{
"results": [
{
"locations": [
{
"latLng": {
"lng": -76.85165,
"lat": 39.25108
},
"adminArea4": "Howard County",
"adminArea5Type": "City",
"adminArea4Type": "County",
This is what I have so far in Java to pull it out:
public class parkJSON
{
public latLng _latLng;
public static class latLng
{
private String _lat, _lng;
public String getLat() { return _lat; }
public String getLon() { return _lng; }
}
}
and
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
parkJSON geo = mapper.readValue(parse, parkJSON.class);
System.out.println(mapper.writeValueAsString(geo));
String lat = geo._latLng.getLat();
String lon = geo._latLng.getLon();
output = lat + "," + lon;
System.out.println("Found Coordinates: " + output);
RESOLVED This is how I solved the issue by using Tree Model for future reference:
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNode rootNode = mapper.readTree(parse);
JsonNode firstResult = rootNode.get("results").get(0);
JsonNode location = firstResult.get("locations").get(0);
JsonNode latLng = location.get("latLng");
String lat = latLng.get("lat").asText();
String lng = latLng.get("lng").asText();
output = lat + "," + lng;
System.out.println("Found Coordinates: " + output);
Jackson JSON - Edit JSON DocumentreadAllBytes(Paths. get("employee. txt")); ObjectMapper objectMapper = new ObjectMapper(); //create JsonNode JsonNode rootNode = objectMapper. readTree(jsonData); //update JSON data ((ObjectNode) rootNode).
A JsonNode is Jackson's tree model for JSON and it can read JSON into a JsonNode instance and write a JsonNode out to JSON. To read JSON into a JsonNode with Jackson by creating ObjectMapper instance and call the readValue() method. We can access a field, array or nested object using the get() method of JsonNode class.
The process of parsing JsonString into JsonNode is very simple. We simply create an instance of ObjectMapper class and use its readTree() method in the following way: String str = "{\"proId\":\"001\",\"proName\":\"MX Pro 20\",\"price\":\"25k\"}"; ObjectMapper map = new ObjectMapper();
If all you're really interested in in this input structure are lat and lng full mapping is probably the least adapted of the different approaches offered by Jackson, as it forces you to write classes to represent the different layers in your data.
There are two alternatives offered by Jackson that will allow you to extract these fields without having to define these classes:
The Jackson documentation contains examples for both techniques, applying them in your program should not be too hard, use your debugger to investigate the data structures created by the parser to see how the document got mapped.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With