Search code examples
javaandroidjsonjson-api

JSON parse for TFL Open Data


Hello everyone I'm trying to parse a JSON file from this LINK.

The returned JSON is as follows-

enter image description here

I need to loop through all the instances of journeys first, then I have to loop through legs for each journeys to get the detailed instruction. As you can see each legs consists of instruction in parts which returns a string, my ultimate goal is to combine these string for each journeys and display them as a TextView. So for the above JSON the end goal is to display -

Jubilee line towards Stratford, or North Greenwich

Northern line towards Edgware, or High Barnet

Till now I've been trying to navigate through this JSON without any luck.

Here is the code I've been working 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();
              }

I believe my approach is wrong and I didn't get the loop right.. Suggestions to parse and navigate and any other approach will be highly appreciated!

Thanks!

UPDATE:

enter image description here enter image description here


Solution

  • 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
            }