I am new to android development.In my application i am using google place api to get the lat and long from the address.But i am stuck at parsing the json as i dont know how to parse such kind of json.

JSON

{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "Kurla West",
                    "short_name": "Kurla West",
                    "types": [
                        "sublocality_level_2",
                        "sublocality",
                        "political"
                    ]
                },
                {
                    "long_name": "Mumbai",
                    "short_name": "Mumbai",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "Mumbai Suburban",
                    "short_name": "Mumbai Suburban",
                    "types": [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    "long_name": "Maharashtra",
                    "short_name": "MH",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "India",
                    "short_name": "IN",
                    "types": [
                        "country",
                        "political"
                    ]
                }
            ],
            "formatted_address": "Kurla West, Mumbai, Maharashtra, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 19.0894645,
                        "lng": 72.89236650000001
                    },
                    "southwest": {
                        "lat": 19.0587919,
                        "lng": 72.87059769999999
                    }
                },
                "location": {
                    "lat": 19.0707532,
                    "lng": 72.8781322
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 19.0894645,
                        "lng": 72.89236650000001
                    },
                    "southwest": {
                        "lat": 19.0587919,
                        "lng": 72.87059769999999
                    }
                }
            },
            "types": [
                "sublocality_level_2",
                "sublocality",
                "political"
            ]
        }
    ],
    "status": "OK"
}

Code which i tried.

jsonObject = new JSONObject(response);

                            if (jsonObject.get("results") instanceof JSONObject) {


                            } else if (jsonObject.get("results") instanceof JSONArray) {
                                jsonArray = jsonObject.getJSONArray("results");

                                for (int j = 0; j < jsonArray.length(); j++) {
                                    jsonObject = jsonArray.getJSONObject(i);

                                    latitude = jsonObject.getJSONObject("location").getDouble("lat");
                                }
                            }



                        } catch (JSONException f) {

                            Log.e("Json exception", f.getMessage());


                        }

EXCEPTION

No value for location

Please do help me out to parse this json,Thanks

Related posts

Recent Viewed