【发布时间】:2016-11-25 07:57:09
【问题描述】:
大家好,我正在尝试从 LINK 解析 JSON 文件。
返回的JSON如下-
我需要先循环遍历 journeys 的所有实例,然后我必须循环遍历每个 journeys 的 legs 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