2012-02-05 1 views
1

Google지도의 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

관련 문제