2016-07-21 10 views
1

Hallo an alle, ich versuche, eine JSON-Datei von dieser LINK zu analysieren.JSON Pars für TFL Open Data

Die zurück JSON ist als folgt-

enter image description here

ich Schleife muß durch alle Instanzen von journeys zuerst, dann habe ich eine Schleife durch legs für jeden journeys zu erhalten die detailed Anweisung. Wie Sie jeder sehen kann legs besteht aus instruction in Teilen, die eine string zurückkehrt, ist mein Ziel, diese string für jede Fahrten zu kombinieren und sie als TextView anzuzeigen. Also für die oben JSON das Endziel anzuzeigen ist -

Jubilee Linie in Richtung Stratford, oder North Greenwich

Northern Linie Richtung Edgware oder High Barnet

Bis jetzt Ich habe versucht, ohne Glück durch diese JSON zu navigieren. Hier

ist der Code, den ich gearbeitet habe on-

try { 
     //object is the JSON file. 
     JSONArray Journey = object.getJSONArray("journeys"); 
     if (Journey != null) { 
     //Retrieving number of possible routes. 
     for (int i=0;i<Journey.length();i++){ 
      Routes.add(Journey.getJSONObject(i)); 
     } 
     //Retrieving number of possible legs for each route. 
     if (!Routes.isEmpty()){ 
     for (int j = 0; j< Routes.size(); j++){ 
      Legs.add(j, Routes.get(j).getJSONArray("legs")); 
     } 
     //Trying to retrieve the detailed instruction here and failing. 
     for(int k=0;k<Routes.get(k).getJSONArray("legs").length();k++){ 
      instructionDetail.add(k,Legs.get(k).getJSONObject(k).getJSONObject("instruction")); 

     } 
    } 
} 

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

ich meinen Ansatz glaube, ist falsch und ich habe nicht die Schleife richtig .. Vorschläge erhalten zu analysieren und zu navigieren und anderer Ansatz wird hoch geschätzt!

Danke!

UPDATE:

enter image description here enter image description here

Antwort

1
JSONArray journeys = new JSONObject(""); 
     for(int i = 0 ; i < journeys.length() ; i++) { // Traverse journeys 
      JSONObject journey = journeys.getJSONObject(i); // get journeys(i) -> journey 
      if(journey.has("legs")) { // if journey has "legs" key 
       JSONArray legs = journey.getJSONArray("legs"); // get the legs array from journey object 
       for(int j = 0 ; j < legs.length() ; j++) { // Traverse legs 
        JSONObject leg = legs.getJSONObject(j); // get legs(j) -> leg 
        if(leg.has("instruction")) { // if leg has "instruction" key in it 
         JSONObject instruction = leg.getJSONObject("instruction"); // get instruction jsonObject 
         String detailed = instruction.optString("detailed", "Fallback detailed"); // get detailed string in instruction object 
        } 
       } 
      } 
     } 

Update:

private static class Detail { 
     String journeyType; 
     String legType; 
     String instructionType; 
     String detail; 

     public Detail(String journeyType, String legType, String instructionType, String detail) { 
      this.journeyType = journeyType; 
      this.legType = legType; 
      this.instructionType = instructionType; 
      this.detail = detail; 
     } 
    } 

... ...

List<Detail> detailList = new ArrayList<>(); 
     JSONArray journeys = new JSONObject(""); 
     for(int i = 0 ; i < journeys.length() ; i++) { // Traverse journeys 
      JSONObject journey = journeys.getJSONObject(i); // get journeys(i) -> journey 
      if(journey.has("legs")) { // if journey has "legs" key 
       JSONArray legs = journey.getJSONArray("legs"); // get the legs array from journey object 
       for(int j = 0 ; j < legs.length() ; j++) { // Traverse legs 
        JSONObject leg = legs.getJSONObject(j); // get legs(j) -> leg 
        if(leg.has("instruction")) { // if leg has "instruction" key in it 
         JSONObject instruction = leg.getJSONObject("instruction"); // get instruction jsonObject 
         String journeyType = journey.getString("$type"); 
         String legType = leg.getString("$type"); 
         String instructionType = instruction.getString("$type"); 
         String detailed = instruction.getString("detailed"); // get detailed string in instruction object 
         detailList.add(new Detail(journeyType, legType, instructionType, detailed)); 
        } 
       } 
      } 
     } 
     for(Detail detail : detailList) { 
      TextView textView = new TextView([yourContext]); 
      textView.setText(detail.detail); 
      yourContentViewGroup.addView(textView); 
      // or you can use View.inflate(context, layoutRes, yourContentViewGroup) and design a layout to show other detail instance values 
     } 
+0

Sorry, ich habe das Instruktionsobjekt verpasst, ich habe die Antwort bearbeitet. –

+0

Danke! Kannst du mir den Code bitte etwas erklären? Ich bin ein Neuling! – envyM6

+0

Ja, nachdem Sie das Reisearray erhalten haben, durchlaufen Sie es und prüfen jedes json-Objekt, ob es "Beine" darin hat. Dann erhalten Sie die Beine Array und durchlaufen Sie es in einer anderen Schleife und überprüfen Sie, ob es eine "Anweisung darin hat. Wenn Sie es finden, erhalten Sie das Befehlsobjekt und erhalten Sie die detaillierte Zeichenfolge. OptString ist ähnlich wie getString, außer Sie können ein Fallback geben it. –