2017-09-26 29 views
-2

私はプログラミングが初めてで、Googleマップの指示に従ってデータを取得するアプリケーションを作成したいと思います。最初のユーザーはリストビューで利用可能なルートを取得する必要があります。複数のオブジェクトと配列を持つJSONのデータを取得する方法

私のJSONデータが、これは、配列の最初のルートである。この

{ 
    geocoded_waypoints: [], 
    routes: [ 
    { 
     bounds: { 

    }, 
    copyrights: "Map data ©2017 Google", 
    legs: [ 
    { 
     arrival_time: {}, 
     departure_time: {}, 
     distance: {}, 
     duration: { 
     text: "25 mins", 
     value: 1514 
     }, 
     end_address: "destination address, Croatia", 
     end_location: {}, 
     start_address: "start address", 
     start_location: {}, 
     steps: [ 
     { 
      distance: {}, 
      duration: {}, 
      end_location: {}, 
      html_instructions: "Walk to Vjesnik", 
      polyline: {}, 
      start_location: {}, 
      steps: [], 
      travel_mode: "WALKING" 
     }, 
     { 
      distance: {}, 
      duration: {}, 
      end_location: {}, 
      html_instructions: "Tram towards Dubec", 
      polyline: {}, 
      start_location: {}, 
      transit_details: { 
      arrival_stop: {}, 
      arrival_time: {}, 
      departure_stop: {}, 
      departure_time: {}, 
      headsign: "Dubec", 
      line: { 
       agencies: [ 
       { 
        name: "Zagrebački Električni Tramvaj", 
        phone: "011 385 60 100 001", 
        url: "http://www.zet.hr/" 
       } 
       ], 
       color: "#ffffff", 
       name: "Savski most - Dubec", 
       short_name: "4", 
       text_color: "#400000", 
       vehicle: {} 
      }, 
      num_stops: 9 
      }, 
      travel_mode: "TRANSIT" 
     }, 

ある

私はいくつかのコードを見つけたが、私はせん断いないです、これは正しいですが、私は

に取得する必要があります
duration: { 
text: "25 mins", 
value: 1514 
}, 
  • これは、脚の配列である

階段配列では、私はオブジェクトtransit_detailsが2つのオブジェクト行とnum_stopsを持っている必要があり、オブジェクト行には、オブジェクト、名前、short_name、および配列代理店があります。

私はこのコードが自分の状況を形成しているかどうかはわかりませんが、私は2つの配列、親と子を取得する必要があることを認識しました。 この質問に既に質問がありましたらお詫び申し上げますが、このデータを取得する方法を教えてください。私が発見した

Javaコードはこれですが、私はそれを修正しましたし、いくつかのコメントを入れている:

public class DirectionsJSONParser { 

    /** Receives a JSONObject and returns a list of lists containing latitude and longitude */ 
    public List<List<HashMap<String,String>>> parse(JSONObject jObject){ 

     List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String,String>>>() ; 
     JSONArray jRoutes = null; 
     JSONArray jLegs = null; 
     JSONObject jSteps = null; 

     try { 

      jRoutes = jObject.getJSONArray("routes"); 

      /** Traversing all routes */ 
      for(int i=0;i<jRoutes.length();i++){ 
       jLegs = ((JSONObject)jRoutes.get(i)).getJSONArray("legs"); 
       List path = new ArrayList<HashMap<String, String>>(); 


       /** Traversing all legs */ 
       for(int j=0;j<jLegs.length();j++){ 
        jSteps = ((JSONObject)jLegs.get(j)).getJSONArray("steps"); 
        jLegs = ((JSONObject)jRoutes.get(j)).getJSONArray("duration"); // --> this is my 

        /** Traversing all steps */ 
        for(int k=0;k<jSteps.length();k++){ 
         String polyline = ""; 
         polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points"); 
         List list = decodePoly(polyline); 
         jSteps = ((JSONObject)jSteps.get(k)).getJSONObject("transit_details"); // this is my 
         jSteps = ((JSONObject)jSteps.get(k)).getJSONObject("line"); // this is my 
         jSteps = ((JSONObject)jSteps.get(k)).getJSONObject("name"); // this is my 
         jSteps = ((JSONObject)jSteps.get(k)).getJSONObject("short_name"); // this is my 

        } 
        routes.add(path); 
       } 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     }catch (Exception e){ 
     } 

     return routes; 
    } 

    /** 
    * Method to decode polyline points 
    * Courtesy : http://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java 
    * */ 
    private List decodePoly(String encoded) { 

     List poly = new ArrayList(); 
     int index = 0, len = encoded.length(); 
     int lat = 0, lng = 0; 

     while (index < len) { 
      int b, shift = 0, result = 0; 
      do { 
       b = encoded.charAt(index++) - 63; 
       result |= (b & 0x1f) << shift; 
       shift += 5; 
      } while (b >= 0x20); 
      int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
      lat += dlat; 

      shift = 0; 
      result = 0; 
      do { 
       b = encoded.charAt(index++) - 63; 
       result |= (b & 0x1f) << shift; 
       shift += 5; 
      } while (b >= 0x20); 
      int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
      lng += dlng; 

      LatLng p = new LatLng((((double) lat/1E5)), 
        (((double) lng/1E5))); 
      poly.add(p); 
     } 

     return poly; 
    } 
} 

私は私が必要とするデータを取得しておりません。

私を助けてくれてありがとう。

+0

このブログを見てくださいhttp://wptrafficanalyzer.in/blog/driving-distance-and-travel-time-duration-between-two-locations-in-google-map-android-api-v2/距離と所要時間が表示されます両方とも – gaurang

+0

完全な応答を送信 –

答えて

0

あなたはJavaでJSONデータを解析するgsonのようなJSON-APIを使用することができます

+0

OK私はgsonを見つけました。コードを使ってデータを取得しました: – Milan

0

は、しかし、リストビューにそれらを置く方法を 私のコードは私のジgsonを使用してデータを取得することですOK:

private void extractTransitDetails(TransitDetailsWrapper transitDetailsWrapper) { 
    List<Route> routes = transitDetailsWrapper.getRoutes(); 
    routes.size(); 
    for (Route route : routes) { 
     List<Leg> legs = route.getLegs(); 
     route.getOverviewPolyline(); 
     for (Leg leg : legs) { 
      List<Step> steps = leg.getSteps(); 
      leg.getDuration(); // vrijeme putovanja 
      leg.getDistance(); // udaljenost u km 
      for (Step step : steps) { 
       TransitDetails transitDetails = step.getTransitDetails(); 

       if (transitDetails != null) { 
        transitDetails.getLine(); 
        if (transitDetails.getLine() != null) { 
         transitDetails.getLine().getShortName(); 
         transitDetails.getLine().getName(); 
        } 
        transitDetails.getNumStops(); 
        transitDetails.getArrivalStop().getName(); 
        transitDetails.getDepartureStop().getName(); 
        transitDetails.getLine().getAgencies(); 

       } 

       Log.d("test", "majlajs"); 

      } 
     } 
    } 


} 
関連する問題