How to parse nested JSON from Map<String, Object>

12,027
JSONObject obj=new JSONObject(jsonresult);
// get result array
JSONArray resultsarray= obj.getJSONArray("results"); 
for (int i=0;i<resultsarray.length(),i++){
        // get Objects using index
        JSONObject jsonobject= results.getJSONObject(i);
        // get geometry object
        JSONObject geometry= jsonobject.getJSONObject("geometry");
        // get location object from geometry
        JSONObject location= geometry.getJSONObject("location");

        // get location values from location object
        double lat = location.optDouble("lat",0.0);
        double long = location.optDouble("lng",0.0);
 }

About optDouble

public double optDouble(String key, double defaultValue) {

Get an optional double associated with a key, or the defaultValue if there is no such key or if its value is not a number. If the value is a string, an attempt will be made to evaluate it as a number.

Share:
12,027
Yonkee
Author by

Yonkee

Updated on July 17, 2022

Comments

  • Yonkee
    Yonkee almost 2 years

    I am using the below to map a json response to a Map

     Map<String, Object> apiResponse = restTemplate.postForObject("https://maps.googleapis.com/maps/api/geocode/json?address="+defaultLocation+"&key="+API_KEY, httpEntity, Map.class, Collections.EMPTY_MAP);
    

    I can use the below to output the entire JSON to a string

        String jsonResponse = apiResponse.get("results").toString();
    

    However, what I want to get is a nested value which is results->geometry->location

    I have tried a number of solution with JSONArrays, JSONObjects, Substring but can't get them to work.

    Response JSON:

    {
       "results" : [
          {
             "address_components" : [
                {
                   "long_name" : "Auckland",
                   "short_name" : "Auckland",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Auckland",
                   "short_name" : "Auckland",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Auckland",
                   "short_name" : "Auckland",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "New Zealand",
                   "short_name" : "NZ",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "Auckland, New Zealand",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : -36.660571,
                      "lng" : 175.287137
                   },
                   "southwest" : {
                      "lat" : -37.065475,
                      "lng" : 174.4438016
                   }
                },
                "location" : {
                   "lat" : -36.8484597,
                   "lng" : 174.7633315
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : -36.660571,
                      "lng" : 175.287137
                   },
                   "southwest" : {
                      "lat" : -37.065475,
                      "lng" : 174.4438016
                   }
                }
             },
             "place_id" : "ChIJ--acWvtHDW0RF5miQ2HvAAU",
             "types" : [ "locality", "political" ]
          },
          {
             "address_components" : [
                {
                   "long_name" : "Auckland",
                   "short_name" : "Auckland",
                   "types" : [ "political", "sublocality", "sublocality_level_1" ]
                },
                {
                   "long_name" : "Auckland",
                   "short_name" : "Auckland",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Auckland",
                   "short_name" : "Auckland",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Auckland",
                   "short_name" : "Auckland",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "New Zealand",
                   "short_name" : "NZ",
                   "types" : [ "country", "political" ]
                },
                {
                   "long_name" : "1010",
                   "short_name" : "1010",
                   "types" : [ "postal_code" ]
                }
             ],
             "formatted_address" : "Auckland, 1010, New Zealand",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : -36.8364659,
                      "lng" : 174.7838398
                   },
                   "southwest" : {
                      "lat" : -36.8621041,
                      "lng" : 174.7503805
                   }
                },
                "location" : {
                   "lat" : -36.8484597,
                   "lng" : 174.7633315
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : -36.8364659,
                      "lng" : 174.7838398
                   },
                   "southwest" : {
                      "lat" : -36.8621041,
                      "lng" : 174.7503805
                   }
                }
             },
             "place_id" : "ChIJuZqpSPtHDW0R4LOiQ2HvAAU",
             "types" : [ "political", "sublocality", "sublocality_level_1" ]
          }
       ],
       "status" : "OK"
    }
    

    Any help would be greatly appreciated.

  • Yonkee
    Yonkee over 7 years
    I have implemented this, but still unsure how to get nested values? JSONHelper jsonHelper = new JSONHelper(apiResponse.toString());
  • Yonkee
    Yonkee over 7 years
    Which JSONObject are you using? Mine doesn't support getJSONArray
  • Pavneet_Singh
    Pavneet_Singh over 7 years
    @Yonkee i am using org.json.JSONObject
  • Yonkee
    Yonkee over 7 years
    Boom, its working. I think I was using the wrong JSON library during my investigation. Thanks for your help.
  • Pavneet_Singh
    Pavneet_Singh over 7 years
    @Yonkee i am glad that it worked for you :) happy coding