json-simple Trying to get specific value from JSON

Catfish picture Catfish · Aug 29, 2013 · Viewed 14.4k times · Source

I'm trying to geocode an address and get the lat/lng coordinates in java. I'm able to geocode the address and have it return in a json object, but I'm unable to figure out how to use json-simple to parse the json below to get the lat/lng. Any help is appreciated.

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Minneapolis",
               "short_name" : "Minneapolis",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Hennepin County",
               "short_name" : "Hennepin County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Minnesota",
               "short_name" : "MN",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Minneapolis, MN, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 45.05125,
                  "lng" : -93.193794
               },
               "southwest" : {
                  "lat" : 44.890144,
                  "lng" : -93.32916299999999
               }
            },
            "location" : {
               "lat" : 44.983334,
               "lng" : -93.26666999999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 45.05125,
                  "lng" : -93.193794
               },
               "southwest" : {
                  "lat" : 44.890144,
                  "lng" : -93.32916299999999
               }
            }
         },
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

I've tried many different things, but this is my latest failure:

JSONObject json = (JSONObject)parser.parse(addressAsString);
JSONObject results = (JSONObject) json.get("results");    <-- LINE 186
JSONArray geometry = (JSONArray) results.get("geometry");

for(int i = 0; i < geometry.size(); i++) {
    JSONObject p = (JSONObject) geometry.get(i);
    System.out.println(p);
}

It produces this stacktrace:

Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject
    at com.AddressGeocoder.geocode(AddressGeocoder.java:186)
    at com.AddressGeocoder.<init>(AddressGeocoder.java:48)
    at com.Geocoder.main(Geocoder.java:7)

Answer

Bengt picture Bengt · Aug 29, 2013

"results" is a JSONArray filled with (in this example just one) JSONObjects. These JSONObjects contain your expected JSONArray "geometry". Following is your code modified, so it loops through the results and printing the geometry data:

JSONObject json = (JSONObject)parser.parse(addressAsString);
JSONArray results = (JSONArray) json.get("results");
for (int i = 0; i < results.size(); i++) {
    // obtaining the i-th result
    JSONObject result = (JSONObject) results.get(i);
    JSONObject geometry = (JSONObject) result.get("geometry");
    JSONObject location = (JSONObject) geometry.get("location");
    System.out.println(location.get("lat"));
    System.out.println(location.get("lng"));
}