如何从json获取lat和lng的值

时间:2015-06-18 08:48:17

标签: java android json

我有一个json响应。我想从json响应中获取lat和lng的值。但是我没有得到这些值。请帮助我。下面是我的回复。

{
    "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"
}

这是我用来获取医院名单的google api回复.Anybode plese帮助我。谢谢你。

4 个答案:

答案 0 :(得分:0)

使用以下步骤:

  1. 为该Json Response
  2. 创建一个模型类
  3. 使用Gson解析响应
  4. 然后创建一个类
  5. 的对象
  6. 使用对象从类
  7. 获取数据变量

答案 1 :(得分:0)

我希望我能提供帮助。

首先,使用http://jsonlint.com/

验证您的JSON

其次,使用此网站生成POJO:http://www.jsonschema2pojo.org/ 确保单击Annotation GSON和Source type JSON! 将课程复制到项目中。

第三:在Android中使用GSON :)(Retrofit对此有利)

答案 2 :(得分:0)

假设您使用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");

答案 3 :(得分:0)

试试这个

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"));
                                }
                            }