2017-07-07 105 views
0

我正在應用程序中,我需要顯示所有的ATM。我從Google API獲取數據,但我無法獲取所有數據。我想獲取latitudelongitude,但是當我獲取數據時,我只能獲得一個值。如何在JSONARRAY中從JSONOBJECT獲取值?

String url = "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=23.308003,81.3275914&radius=5000&type=atm&key=AIzaSyCYZoSkxHC_Exym4YBWvXZXwMyJA7dzEB4"; 
     try { 
      JSONObject jObject = new JSONObject(request(url)); 
      String jj = jObject.getString("status"); 

      JSONArray data = jObject.getJSONArray("results"); 

      location = new ArrayList<HashMap<String, String>>(); 
      HashMap<String, String> map; 

      for(int i = 0; i < data.length(); i++){ 
       JSONObject c = data.getJSONObject(i); 
       map = new HashMap<String, String>(); 

      JSONObject geometry = c.getJSONObject("geometry"); 
      JSONObject locate = geometry.getJSONObject("location"); 
      String lat = locate.getString("lat"); 
      String lng = locate.getString("lng"); 
      Log.e("Data", lat+"--"+lng); 
      //map.put("LocationID", c.getString("id")); 
      //map.put("placeId",c.getString("place_id")); 
      //location.add(map); 


      } 

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

後你還沒有把你從Json的在位置得到緯度經度的值( HashMap) –

+0

@ user3810679我建議您閱讀有關Retrofit和GSON/Jackson的信息 – DeKaNszn

+0

Possibl e [發送和解析JSON對象]的副本(https://stackoverflow.com/questions/2818697/sending-and-parsing-json-objects) –

回答

4

我會建議你通過一些解析教程。稍後你可以像我們這樣的圖書館或傑克森來解析你的json。現在很多人不會自己寫json解析代碼。

一旦你瞭解了json結構,你應該可以自己解析它。

您的JSON

"results": [ // array 
    {  // jsonobject 
     "geometry": { // geometry jsonobject 
      "location": { // location jsonobject 
       // values 
       "lat": 23.3085793, 
       "lng": 81.353116 
      } 
     }, 
     ... // the rest of the json 

然後

// this is right 
JSONArray data = jObject.getJSONArray("results"); 

for(int i = 0; i < data.length(); i++){ 
     JSONObject c = data.getJSONObject(i); 
     JSONObject geometry = c.getJSONObject("geometry"); 
     JSONObject location = geometry.getJSONObject("location"); 
     String lat = location.getString("lat"); 
     String lng = location.getString("lng"); 

     Log.d("Location->"," "+lat+" "+lng); 
     // now do what you want. 
     // You probably meant to do 
     map = new HashMap<String, String>(); 
     map.put("lat",lat); 
     map.pt("lng",lng); 
     location.add(map); 

    } 
+0

我無法獲取所有的經度和緯度值。我得到的經緯度只有 – user3810679

+0

@ user3810679,你將獲得所有的值。 'Log.e(「Location->」,「」+ lat +「」+ lng);'在上面的循環中添加此代碼 – Raghunandan

+0

我已經使用過同樣的東西,但我沒有得到如何獲取所有經度和緯度值 – user3810679

相關問題