2012-02-05 97 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