2013-01-21 2 views
0

내 JSON 파일에서 "lat"및 "lng"를 가져 와서 GeoPoint를 유지하는 하나의 배열에 추가하고 싶습니다. 나는 그런 식으로 일을하려고했지만 나를 위해 작동하지 않습니다 :JSON 파일에서 lng 및 lat를 얻는 방법

{ 
     "routes" : [ 
      { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 41.87999000000001, 
        "lng" : -87.615020 
       }, 
       "southwest" : { 
        "lat" : 29.74674000000001, 
        "lng" : -95.361220 
       } 
      }, 
      "copyrights" : "Dane do Mapy ©2013 Google", 
      "legs" : [ 
       { 
        "distance" : { 
         "text" : "1 085 mil", 
         "value" : 1746457 
        }, 
        "duration" : { 
         "text" : "16 godz. 39 min", 
         "value" : 59955 
        }, 
        "end_address" : "1362 Chenevert Street, Houston, Teksas 77003, Stany Zjednoczone", 
        "end_location" : { 
         "lat" : 29.750110, 
         "lng" : -95.36016000000001 
        }, 
        "start_address" : "138-230 South Columbus Drive, Chicago, Illinois 60601, Stany Zjednoczone", 
        "start_location" : { 
         "lat" : 41.87999000000001, 
         "lng" : -87.62075000000002 
        }, 
        "steps" : [ 
         { 
         "distance" : { 
          "text" : "338 stóp", 
          "value" : 103 
         }, 
         "duration" : { 
          "text" : "1 min", 
          "value" : 9 
         }, 
         "end_location" : { 
          "lat" : 41.88090, 
          "lng" : -87.62069000000001 
         }, 
         "html_instructions" : "Kieruj się \u003cb\u003eS Columbus Dr\u003c/b\u003e na \u003cb\u003epółnoc\u003c/b\u003e w stronę \u003cb\u003eE Monroe St\u003c/b\u003e", 
         "polyline" : { 
          "points" : "}[email protected]@@QMUA" 
         }, 
         "start_location" : { 
          "lat" : 41.87999000000001, 
          "lng" : -87.62075000000002 
         }, 
         "travel_mode" : "DRIVING" 
         }, 
         { 
         "distance" : { 
          "text" : "0,2 mil", 
          "value" : 266 
         }, 
         "duration" : { 
          "text" : "1 min", 
          "value" : 33 
         }, 
         "end_location" : { 
          "lat" : 41.88086000000001, 
          "lng" : -87.61750000000001 
         }, 
         "html_instructions" : "Skręć \u003cb\u003ew prawo\u003c/b\u003e w \u003cb\u003eE Monroe St\u003c/b\u003e", 
         "polyline" : { 
          "points" : "[email protected][email protected]@[email protected][email protected][email protected]" 
         }, 
         "start_location" : { 
          "lat" : 41.88090, 
          "lng" : -87.62069000000001 
         }, 
         "travel_mode" : "DRIVING" 
         }, 
         { 
         "distance" : { 
          "text" : "2,0 mil", 
          "value" : 3186 
         }, 
         "duration" : { 
          "text" : "3 min", 
          "value" : 180 
         }, 
         "end_location" : { 
          "lat" : 41.85320, 
          "lng" : -87.61470000000001 
         }, 
         "html_instructions" : "Skręć \u003cb\u003ew prawo\u003c/b\u003e w \u003cb\u003eU.S. 41 S\u003c/b\u003e", 
         "polyline" : { 
          "points" : "[email protected]`PO|[email protected]?lDCZAfMGjAA`@[email protected]?^[email protected]@[email protected]@\\[email protected]^[email protected]@[email protected]@[email protected]@[email protected]`@[email protected]@[email protected]`[email protected]@[email protected]@[email protected][email protected]@[email protected][email protected][email protected]?PEvBWxB][email protected]][email protected]@[email protected]@[email protected][email protected]]|@][email protected]`F{[email protected]" 
         }, 
         "start_location" : { 
          "lat" : 41.88086000000001, 
          "lng" : -87.61750000000001 
         }, 
         "travel_mode" : "DRIVING" 
         }, 
         { 
         "distance" : { 
          "text" : "0,4 mil", 
          "value" : 581 
         }, 
         "duration" : { 
          "text" : "1 min", 
          "value" : 30 
         }, 
         "end_location" : { 
          "lat" : 41.848390, 
          "lng" : -87.614670 
         }, 
         "html_instructions" : "Zjedź \u003cb\u003eInterstate 55 S\u003c/b\u003e w kierunku \u003cb\u003eSaint Louis\u003c/b\u003e", 
         "polyline" : { 
          "points" : "[email protected]@[email protected]?JEDAVI`A[[email protected]`A[[email protected]@E\\[email protected]@VDJBHBTFTHRJPNRNLL\\[email protected]\\BF" 
         }, 
         "start_location" : { 
          "lat" : 41.85320, 
          "lng" : -87.61470000000001 
         }, 
         "travel_mode" : "DRIVING" 
         }, 

사람이 내가 다른 그것을 할 수있는 방법을 알고 있습니까 : 여기

protected List<GeoPoint> JsonArray(){ 
     List<GeoPoint> endp = new ArrayList<GeoPoint>(); 

     try{ 
     JSONObject obj = new JSONObject(json); 
     JSONArray steps = obj.getJSONArray("routes"); 
     for(int i=0;i<steps.length();i++){ 
      JSONObject temp = steps.getJSONObject(i); 
      JSONObject ele = temp.optJSONObject("steps").optJSONObject("end_location"); 
      ele.getJSONObject("lat"); 
      ele.getJSONObject("lng"); 

      double lat = Double.parseDouble(ele.getJSONObject("lat").toString()); 
      double lng = Double.parseDouble(ele.getJSONObject("lng").toString()); 
      endp.add(new GeoPoint((int)(lat *1E6),(int)(lng * 1E6))); 

     } 



     }catch (JSONException e) { 
      // TODO: handle exception 
     } 
     return endp; 
    } 

내 JSON 파일을 보는 방법의 일부입니다 방법?

+0

PLZ는 전체 유효 JSON –

+0

를 게시 내가 거기 모두를 추가 할 수 있지만, 여기 당신이 내 JSON 파일에 대한 링크가 http://maps.google.com/maps/api/directions /json?origin=41.88,-87.62&destination=29.75,-95.36&sensor=true&mode=driving – carolajnn

+0

내 대답 참조 ... –

답변

3

변수 JSONArray steps = obj.getJSONArray("routes");은 JSON 객체의 "steps"부분이 아니라 "routes"부분을 나타냅니다.

또한 JSON 계층의 일부 레벨을 건너 뜁니다. JSON parsing of Google Maps API in Android App

1

로 현재 JSON 문자열을 구문 분석 : 주어진 JSON으로, 당신은

JSONArray routes = obj.getJSONArray("routes"); 
for(int i=0; i < routes.length(); i++){ 
    // Grab the first route 
    JSONObject route = routesArray.getJSONObject(i); 
    // Take all legs from the route 
    JSONArray legs = route.getJSONArray("legs"); 
    // Grab first leg 
    JSONObject leg = legs.getJSONObject(0); 
    // Take all steps from the leg 
    JSONArray steps = leg.getJSONArray("steps"); 
    // Grab first step 
    JSONObject step = steps.getJSONObject(0); 

    JSONObject endLocation = step.getJSONObject("end_location"); 
    String lat = endLocation.getString("lat"); 
    String lng = endLocation.getString("lng"); 

참조 (내가 경로 배열을 제외하고, 다른 배열을 통해 반복 생략 한)이 JSON을 통과하려면 다음 단계를 필요 위도 및 LNG하려면 :

JSONObject obj = new JSONObject(json); 
     JSONArray steps = obj.getJSONArray("routes"); 
     for(int i=0;i<steps.length();i++){ 
      JSONObject temp = steps.getJSONObject(i); 

      // get bounds JSONObject 
      JSONObject boundsjsonobj = temp.getJSONObject("bounds"); 
      // get northeast JSONObject 
      JSONObject jsonboj_bounds_northeast= boundsjsonobj.getJSONObject("northeast"); 
      // get northeast lng and lat 
      String str_northeast_lat=jsonboj_bounds_northeast.getString("lat"); 
      String str_northeast_lng=jsonboj_bounds_northeast.getString("lng"); 
      // get southwest JSONObject 
      JSONObject jsonboj_bounds_southwest= boundsjsonobj.getJSONObject("southwest"); 
      // get northeast lng and lat 
      String str_southwest_lat=jsonboj_bounds_southwest.getString("lat"); 
      String str_southwest_lng=jsonboj_bounds_southwest.getString("lng"); 

      //get legs JsonArray from routes 
      JSONArray jsonarray_legs = temp.getJSONArray("legs"); 
      for(int j=0;j<jsonarray_legs.length();j++){ 
       JSONObject jobjlegs = jsonarray_legs.getJSONObject(j); 
       // get end_location json object 

       JSONObject jobjlegs_end_location = jobjlegs.getJSONObject("end_location"); 
        String str_end_location_lat==jobjlegs_end_location.getString("lat"); 
        String str_end_location_lng==jobjlegs_end_location.getString("lng"); 

        // get start_address object 


       JSONObject jobjlegs_start_address = jobjlegs.getJSONObject("start_address"); 
        String str_start_address_lat==jobjlegs_start_address.getString("lat"); 
        String str_start_address_lng==jobjlegs_start_address.getString("lng"); 

        // get steps jsonArray 
        JSONArray jsonarray_steps = jobjlegs.getJSONArray("steps"); 
        for(int k=0;k<jsonarray_steps.length();k++){ 
       JSONObject jobjsteps = jsonarray_steps.getJSONObject(k); 

        // get end_location jsonobject 
        JSONObject jobjsteps_end_location = jobjsteps.getJSONObject("end_location"); 
         double latend = Double.parseDouble(jobjsteps_end_location.getString("lat")); 
         double lngend = Double.parseDouble(jobjsteps_end_location.getString("lng")); 
         endp.add(new GeoPoint((int)(latend *1E6),(int)(lngend * 1E6))); 

        // get start_location jsonobject 
        JSONObject jobjsteps_start_location = jobjsteps.getJSONObject("start_location"); 
         double latstart = Double.parseDouble(jobjsteps_start_location.getString("lat")); 
         double lngstart = Double.parseDouble(jobjsteps_start_location.getString("lng")); 
        } 

      } 

     } 
+0

이 부분을 잘못하고 있습니다. JSONObject obj = new JSONObject (json); 왜냐하면 내가 할 때 디버그, 그것은 바로 여기서 예외로 간다. 특별한 방법으로 JSON 파일에 대한 링크를 만들어야합니까? 이거 열지? – carolajnn

+0

@carolajnn : 구문 분석이 올바르므로 JSONObject (json)에 대한 json URL을 제공하십시오. json 대신 Google 장소 API에서 가져온 문자열을 전달해야합니다. –

+0

http://maps.google.com/maps/api/directions/json?origin=51.49,19.83&destination=51.79,19.44&sensor=true&mode=driving here 당신은 내 URL을 가지고 – carolajnn

관련 문제