JSON 解析 TFL 开放数据
JSON parse for TFL Open Data
大家好,我正在尝试从这个 LINK.
解析一个 JSON 文件
返回的JSON如下-
我需要先遍历journeys
的所有实例,然后我必须遍历legs
为每个 journeys
得到 detailed
指令。如您所见,每个 legs
由 instruction
组成,其中 returns 和 string
,我的最终目标是将每个旅程的这些string
组合起来,并将它们显示为TextView
。所以对于上面的 JSON 最终目标是显示 -
Jubilee line towards Stratford, or North Greenwich
Northern line towards Edgware, or 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();
}
我认为我的方法是错误的,我没有得到正确的循环。非常感谢解析和导航的建议以及任何其他方法!
谢谢!
更新:
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
}
大家好,我正在尝试从这个 LINK.
解析一个 JSON 文件返回的JSON如下-
我需要先遍历journeys
的所有实例,然后我必须遍历legs
为每个 journeys
得到 detailed
指令。如您所见,每个 legs
由 instruction
组成,其中 returns 和 string
,我的最终目标是将每个旅程的这些string
组合起来,并将它们显示为TextView
。所以对于上面的 JSON 最终目标是显示 -
Jubilee line towards Stratford, or North Greenwich
Northern line towards Edgware, or 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();
}
我认为我的方法是错误的,我没有得到正确的循环。非常感谢解析和导航的建议以及任何其他方法!
谢谢!
更新:
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
}