2012-02-05 44 views
1

我從谷歌地圖得到以下JSON響應。我已經給出了兩個郵政編碼作爲開始和目的地。它描述了響應中的路由,以便部分工作。我想從響應中獲得經度和緯度,以便我可以加載相應的地圖。我遇到的問題是無法找到「東北」屬性,有什麼想法?無法從JSON輸出獲取緯度/經度值

02-05 18:50:03.668: E/*********HelloviewActivity(10381): response code OK 
02-05 18:50:03.673: E/*********HelloviewActivity(10381): { 
02-05 18:50:03.673: E/*********HelloviewActivity(10381): "routes" : [ 
02-05 18:50:03.673: E/*********HelloviewActivity(10381):  { 
02-05 18:50:03.673: E/*********HelloviewActivity(10381):   "bounds" : { 
02-05 18:50:03.673: E/*********HelloviewActivity(10381):    "northeast" : { 
02-05 18:50:03.678: E/*********HelloviewActivity(10381):    "lat" : 53.654430, 
02-05 18:50:03.678: E/*********HelloviewActivity(10381):    "lng" : -1.778250 
02-05 18:50:03.678: E/*********HelloviewActivity(10381):    }, 
02-05 18:50:03.678: E/*********HelloviewActivity(10381):    "southwest" : { 
02-05 18:50:03.683: E/*********HelloviewActivity(10381):    "lat" : 53.62041000000001, 
02-05 18:50:03.683: E/*********HelloviewActivity(10381):    "lng" : -1.831710 
02-05 18:50:03.683: E/*********HelloviewActivity(10381):    } 
02-05 18:50:03.683: E/*********HelloviewActivity(10381):   }, 

String jsonOutput = response.toString(); 
     // Log.e(TAG,"jsonOutput = " + jsonOutput); 

     JSONObject results = null; 
     try { 

      results = new JSONObject(jsonOutput); 


      routes = results.getJSONArray("routes"); 


      bounds = routes.getJSONObject(0); 

      northeast = bounds.getJSONObject("northeast"); 


      lat = Double.parseDouble((String) northeast.get("lat")); 


      lon = Double.parseDouble((String) northeast.get("lng")); 

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

     Log.e(TAG, "lon/lat = "+lon +" "+lat); 

02-05 18:50:04.668: W/System.err(10381): org.json.JSONException: JSONObject["northeast"] not found. 

回答

2

routes是(匿名)的對象數組,包含bounds對象。

anonObject = routes.getJSONObject(0); 
bounds = anonObject.getJSONObject("bounds"); 

編輯:此外 - 一旦你得到你的bounds對象; latlngString秒 - 他們已經Double秒 - 你不做任何轉換:

lat = bounds.getDouble("lat"); 
lon = bounds.getDouble("lng"); 
+0

嘿感謝現在的工作:) – turtleboy 2012-02-05 19:50:15