2016-01-04 18 views
0

我在我的android應用中使用谷歌放置api webservice,我是新的android和不能讀取api的JSON結果,我打印這個結果轉換在谷歌放置api web服務在android的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); 
+0

你能發表嗎?你從google api得到的整個json響應? – rodit

+0

發佈您的整個JSON響應和代碼。 –

回答

1

孔U可以解析ŧ他谷歌地方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(); 
} 
+0

非常感謝:)它的工作 –

+0

@Amal Kronz:好:) Plz接受答案。 – kevz

+0

抱歉,我接受它,但互聯網連接中斷:) –

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