在android中转换google place api web服务的json结果

时间:2016-01-04 12:33:56

标签: android json parsing google-api

我在我的Android应用程序中使用google place api webservice并且我是android的新手,无法从api读取JSON结果,我打印此结果

{
    "location": {
        "lng": -73.9834889,
        "lat": 40.7724641
    }
}

{
    "viewport": {
        "southwest": {
            "lng": -74.03514899999999,
            "lat": 40.698078
        },
        "northeast": {
            "lng": -73.90331300000001,
            "lat": 40.820045
        }
    },
    "location": {
        "lng": -73.9712488,
        "lat": 40.7830603
    }
}

当我写这段代码时

JSONObject dataObj = new JSONObject(response);
String status = dataObj.getString("status");
Log.i("status",status);
if (status.equals("OK")) {
    JSONObject jsonArray =dataObj.getJSONObject("results");
    Log.i("result",""+jsonArray);
    JSONArray aa=dataObj.getJSONArray("results");
    Log.i("resultssss",""+aa);
    ArrayList<Mosque> mosque = new ArrayList<Mosque>();
    for (int i = 0; i < aa.length(); i++) {
        JSONObject jsonObject = aa.getJSONObject(i);
        String geometry=jsonObject.getString("geometry");
        Log.i("geometry",geometry);

3 个答案:

答案 0 :(得分:2)

这是可能对您有用的链接..

get coordinates from JSON from google maps

第二个链接链接可能会清除您从JSON获取详细信息的想法。 http://www.tutorialspoint.com/android/android_json_parser.htm

希望它可以帮助你..:D

答案 1 :(得分:1)

你可以解析google places api响应并提取纬度和经度,如下所示。

try {
    JSONObject jObject = new JSONObject(response);
    JSONArray jResults = jObject.getJSONArray("results");

    for (int j = 0; j < jResults.length(); j++) {
        JSONObject jPlaceObj = jResults.getJSONObject(j);
        JSONObject jGeometry =  jPlaceObj.getJSONObject("geometry");
        JSONObject jLocation = jGeometry.getJSONObject("location");

        String lat = jLocation.getString("lat");
        String lng = jLocation.getString("lng");
    }


} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

答案 2 :(得分:0)

try {
        JSONObject jObject = new JSONObject(response);
        JSONArray jResults = jObject.getJSONArray("results");
        JSONObject jPlaceObj = jResults.getJSONObject(0);
        JSONObject jGeometry =  jPlaceObj.getJSONObject("geometry");
        JSONObject jLocation = jGeometry.getJSONObject("location");

        String lat = jLocation.getString("lat");
        String lng = jLocation.getString("lng");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }