Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Jackson JSON Parser: Complex JSON?

Tags:

java

json

jackson

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);
like image 563
Ronnie Dove Avatar asked Feb 23 '13 01:02

Ronnie Dove


People also ask

How do I read a specific JsonNode in Jackson API?

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).

How does Jackson read nested JSON?

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.

How do you parse JSON strings into Jackson JsonNode model?

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();


1 Answers

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:

  1. The tree model offers a number of navigation methods to traverse the tree and extract the data you're interested in.
  2. Simple data binding maps the JSON document onto a Map or a List which can then be navigated with the methods offered by these collections.

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.

like image 86
fvu Avatar answered Oct 26 '22 20:10

fvu