gpt4 book ai didi

java - TFL 开放数据的 JSON 解析

转载 作者:行者123 更新时间:2023-11-30 01:10:02 25 4
gpt4 key购买 nike

大家好,我正在尝试从这个 LINK 解析一个 JSON 文件.

返回的JSON如下-

enter image description here

我需要遍历 journeys 的所有实例 首先,然后我必须遍历 legs 对于每个 journeys 获取 detailed 指令。如您所见,每个 legs instruction 组成 部分返回 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();
}

我认为我的方法是错误的,我没有得到正确的循环。非常感谢解析和导航以及任何其他方法的建议!

谢谢!

更新:

enter image description here enter image description here

最佳答案

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
}

关于java - TFL 开放数据的 JSON 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38509063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com