0

I have a json response.I want to get the value of lat and lng from the json response.But i didn't get the values.Please Help me.Below is my response.

{
    "html_attributions": [],
    "results": [
        {
            "geometry": {
                "location": {
                    "lat": 9.493837,
                    "lng": 76.338506
                }
            },
            "icon": "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
            "id": "2730a3d7ab068d666e61a02ce6160b4cd21a38c7",
            "name": "Nagarjuna",
            "place_id": "ChIJr0-U4vSECDsRtiALUlgZOzI",
            "reference": "CmRcAAAA4yl72_x5llqvdshRJwuuntunXrYu33qdP5G7-I0CdHzcDsyd6wwqjxdNeqvT6vtRIoDoIk_WGNd62SYSoNEdBrpDrOcf5g5eZMj_vobhmF11mrujsQ_Yc7p-oGxQH0XtEhDNJdjQf_WlK_dRAckBzlA3GhQ_wzXs5RxoaxWDSEurm_R5syuovg",
            "scope": "GOOGLE",
            "types": [
                "hospital",
                "establishment"
            ],
            "vicinity": "State Highway 40, Kodiveedu, Alappuzha"
        },
        {
            "geometry": {
                "location": {
                    "lat": 9.500542,
                    "lng": 76.341017
                }
            },
            "icon": "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
            "id": "d5b6c81a53a346dea1263de7a777703bc72b8796",
            "name": "SYDNEY OPTICALS",
            "opening_hours": {
                "open_now": true,
                "weekday_text": []
            },
            "photos": [
                {
                    "height": 422,
                    "html_attributions": [],
                    "photo_reference": "CnRnAAAA_jg-NlSrVKkDOP7wXhPhvFTD8NW4A4aDI_Ptl3F9c_qt9QwdztNTG9Cr51uGIphpEUMyhsTfhhaa-TlfoL8MUEffbguZJ1AhKUwzfe7Mbrvm2KW8Y1EQXVw_3FglxA4LM1hqWJCK_AV4xcvOw1vuHRIQ8_keBYr29H8jK145RQ_PkRoUgPZ0qzcSNdIntc2ZI4WvBIR-TBQ",
                    "width": 630
                }
            ],
            "place_id": "ChIJl9tvIV6ECDsR7Cmf3KkIl-4",
            "reference": "CnRjAAAA3qhFUcb8P9akE8xw-KwfF6OU6qvy2cVX4Sg0qK_xCOfeUEyxoFgwof8rk-Z2BBJ7Z4m7ZTbfdp78wqFbeLfojQWPldq7XDfzX0pLScBSysebEp9P4XmrsAO5qyqSUveb5jWcJDkYiOLKgaKMzoWQphIQbldrdJ9iEDHkGiQ7tleNYxoUnjcjcynUDMftaErRUQbOn-GkWj0",
            "scope": "GOOGLE",
            "types": [
                "store",
                "hospital",
                "health",
                "establishment"
            ],
            "vicinity": "Mullakkal, Alappuzha"
        }
    ],
    "status": "OK"
}

This is the google api response i used for getting the list of hospitals.Anybode plese help me.Thanks in advance.

4 Answers4

0

Use these steps:

  1. Create a model class for that Json Response
  2. Use Gson to parse the response
  3. Then create an object of the class
  4. using the object get the data variable from the class
Devrath
  • 42,072
  • 54
  • 195
  • 297
0

I hope I can help.

First, validate your JSON with http://jsonlint.com/

Second, use this site to generate POJO: http://www.jsonschema2pojo.org/ make sure that Annotation GSON and Source type JSON are clicked ON! Copy your classes in to your project.

Third: use GSON in Android :) (Retrofit is good for this)

narancs
  • 5,234
  • 4
  • 41
  • 60
0

Supposing you use the json.org Implementation for Java:

String response = "{\"html_attributions\": [], \"results\": ...";
JSONObject jo = new JSONObject(response);
JSONObject result = jo.getJSONArray("results").getJSONObject(0);
JSONObject location = result.getJSONObject("geometry").getJSONObject("location");
double lat = location.getDouble("lat");
double lng = location.getDouble("lng");
Ridcully
  • 23,362
  • 7
  • 71
  • 86
0

try this

String response = "{\"html_attributions\": [], \"results\": ...";
                              JSONObject objResponce = new JSONObject(response);
                            JSONArray arrayResults=new JSONArray(objResponce.getString("results"));
                            if(arrayResults.length()>0)
                            {
                                for(int i=0;i<arrayResults.length();i++)
                                {
                                    //--- get each json object from array -----
                                    JSONObject objArrayResults = arrayResults.getJSONObject(i);
                                    //--- get geometry json object from each object of array -----
                                    JSONObject objGeometry=new JSONObject(objArrayResults.getString("geometry"));
                                    //--- get location json object from geometry json object -----
                                    JSONObject objLocation=new JSONObject(objGeometry.getString("location"));
                                    System.out.println("Latitude :"+objLocation.getString("lat"));
                                    System.out.println("Longitude :"+objLocation.getString("lng"));
                                }
                            }
anu
  • 213
  • 1
  • 3
  • 10