【问题标题】:JSON parse for TFL Open DataTFL 开放数据的 JSON 解析
【发布时间】:2016-11-25 07:57:09
【问题描述】:

大家好,我正在尝试从 LINK 解析 JSON 文件。

返回的JSON如下-

我需要先循环遍历 journeys 的所有实例,然后我必须循环遍历每个 journeyslegs strong> 获取 detailed 指令。正如你所看到的,每个 legs 都由 instruction 部分组成,返回一个 string,我的最终目标是结合这些string 用于每个旅程并将其显示为 TextView。所以对于上面的 JSON,最终目标是显示 -

禧年线前往斯特拉特福或北格林威治

向 Edgware 或 High Barnet 的北线

到目前为止,我一直在尝试浏览此 JSON,但没有任何运气。

这是我一直在处理的代码-

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();
              }

我认为我的方法是错误的,我没有正确地循环。解析和导航的建议以及任何其他方法将不胜感激!

谢谢!

更新:

【问题讨论】:

    标签: java android json json-api


    【解决方案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
                        }
                    }
                }
            }
    

    更新:

    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
            }
    

    【讨论】:

    • 对不起,我错过了指令对象,我编辑了答案。
    • 谢谢!你能为我解释一下代码吗?我是新手!
    • 是的,基本上在你得到旅程数组之后,你遍历它并检查每个 json 对象是否有“腿”。然后你得到legs数组并在另一个循环中遍历它并检查它是否有一个“指令。当你找到它时,你得到指令对象并得到详细的字符串。optString类似于getString,除了你可以给一个后备它。
    • 是的,我以为您将详细的字符串放入数组中,然后创建一个与之对应的ui。由于它是一个嵌套循环,因此您还可以访问上层对象,例如该细节的指令信息或腿信息等。这是为您的细节建模并为其创建简单用户界面的更新。
    • 哦,还有一件事,最好的做法是异步创建 detailList,就像从请求中获取结果一样,然后膨胀视图并在 ui 中设置文本视图。最好使用 listview 或 recyclerview,而不是将视图膨胀到容器视图组。
    猜你喜欢
    • 2020-06-14
    • 2019-06-06
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    相关资源
    最近更新 更多