你如何返回的Android以下嵌套JSON数组的内容?(How do you return the

2019-09-28 05:26发布

W / O使用哈希映射或GSON。 这是我第一次解析嵌套数组。 我知道如何解析单个阵列和JSON对象。 我看过几个线程在这里,我立足我就这一个代码:

如何解析在安卓这个嵌套的JSON数组

我解析从以下URL以下JSON数据(从邮差粘贴)。 JSON结构粘贴下面。

https://api.tfl.gov.uk/Line/victoria/Route/Sequence/inbound?serviceTypes=Regular,Night

我想返回的顺序16个地铁站的列表; 返回“ID”:940GDCOELW”或‘naptanId:940DFDDKLJ09’和‘名’:‘沃伦街地铁站’为所有站它们存储在两个‘站’(非连续)和‘stopPointSequences’阵列和也“orderedLineRoutes”。我开始解析“stopPointSequences”,但我不知道如何将数据添加到ArrayList。错误代码上述表示。或者,它会更容易解析“orderedLineRoutes”?但有可能解析它由名称匹配的id?我不知道,如果包含在阵列中的每个“名字”。“stopPointSequence”排列粘贴下面的第一部分。谢谢你在前进。

    {
        "$type": "Tfl.Api.Presentation.Entities.RouteSequence, Tfl.Api.Presentation.Entities",
        "lineId": "victoria",
        "lineName": "Victoria",
        "direction": "inbound",
        "isOutboundOnly": false,
        "mode": "tube", 
        "lineStrings":[..];
         "stations":[..];
        "stopPointSequences":[
 {
            "$type": "Tfl.Api.Presentation.Entities.StopPointSequence, Tfl.Api.Presentation.Entities",
            "lineId": "victoria",
            "lineName": "Victoria",
            "direction": "inbound",
            "branchId": 0,
            "nextBranchIds": [],
            "prevBranchIds": [],
                "stopPoint": [
                    {
                        "$type": "Tfl.Api.Presentation.Entities.MatchedStop, Tfl.Api.Presentation.Entities",
                        "parentId": "HUBWHC",
                        "stationId": "940GZZLUWWL",
                        "icsId": "1000249",
                        "topMostParentId": "HUBWHC",
                        "modes": [
                            "tube"
                        ],
                        "stopType": "NaptanMetroStation",
                        "zone": "3",
                        "hasDisruption": true,
                         "lines": [{..}],
      "status": true,
                        "id": "940GZZLUWWL",
                        "name": "Walthamstow Central Underground Station",
                        "lat": 51.582965,
                        "lon": -0.019885
                    },
    ],
         "orderedLineRoutes": [
            {
                "$type": "Tfl.Api.Presentation.Entities.OrderedRoute, Tfl.Api.Presentation.Entities",
                "name": "Walthamstow Central  ↔  Brixton ",
                "naptanIds": [
                    "940GZZLUWWL",
                    "940GZZLUBLR",
                    "940GZZLUTMH",
                    "940GZZLUSVS",
                    "940GZZLUFPK",
                    "940GZZLUHAI",
                    "940GZZLUKSX",
                    "940GZZLUEUS",
                    "940GZZLUWRR",
                    "940GZZLUOXC",
                    "940GZZLUGPK",
                    "940GZZLUVIC",
                    "940GZZLUPCO",
                    "940GZZLUVXL",
                    "940GZZLUSKW",
                    "940GZZLUBXN"
                ],
                "serviceType": "Night"
            },
            {
                "$type": "Tfl.Api.Presentation.Entities.OrderedRoute, Tfl.Api.Presentation.Entities",
                "name": "Walthamstow Central  ↔  Brixton ",
                "naptanIds": [
                    "940GZZLUWWL",
                    "940GZZLUBLR",
                    "940GZZLUTMH",
                    "940GZZLUSVS",
                    "940GZZLUFPK",
                    "940GZZLUHAI",
                    "940GZZLUKSX",
                    "940GZZLUEUS",
                    "940GZZLUWRR",
                    "940GZZLUOXC",
                    "940GZZLUGPK",
                    "940GZZLUVIC",
                    "940GZZLUPCO",
                    "940GZZLUVXL",
                    "940GZZLUSKW",
                    "940GZZLUBXN"
                ],
                "serviceType": "Regular"          }]
    }},

JSONUTILS类:

     public static ArrayList<Stations> extractFeatureFromStationJson(String stationJSON) {
    // If the JSON string is empty or null, then return early.
    if (TextUtils.isEmpty(stationJSON)) {
        return null;
    }
    ArrayList<Stations> stations = new ArrayList<>();
    try {
        // Create a JSONObject from the JSON response string
        JSONObject baseJsonResponse = new JSONObject(stationJSON);
        JSONArray stopPointSequenceArrayList = baseJsonResponse.getJSONArray("stopPointSequences");
        if (stopPointSequenceArrayList != null) {
            for (int i = 0; i < stopPointSequenceArrayList.length(); i++) {
                JSONObject elem = stopPointSequenceArrayList.getJSONObject(i);
                if (elem != null) {
                    JSONArray stopPointArrayList = elem.getJSONArray("stopPoint");
                    if (stopPointArrayList != null) {
                        for (int j = 0; j < stopPointArrayList.length(); j++) ;
                        JSONObject innerElem = stopPointArrayList.getJSONObject(i);
                        if (innerElem != null) {
                            String idStation = "";
                            if (innerElem.has("id")) {
                                idStation = innerElem.optString(KEY_STATION_ID);
                            }
                            String nameStation = "";
                            if (innerElem.has("name")) {
                                nameStation = innerElem.optString(KEY_STATION_NAME);
                            }
        //Error                    stopPointSequenceArrayList.add(stopPointArrayList);
                        }
                    }
                }
            }
        }
        //Error
        Stations station = new Stations(idStation, nameStation);
        stations.add(station);

    } catch (JSONException e) {
        // If an error is thrown when executing any of the above statements in the "try" block,
        // catch the exception here, so the app doesn't crash. Print a log message
        // with the message from the exception.
        Log.e("QueryUtils", "Problem parsing stations JSON results", e);

    }
      // Return the list of stations
    return stations;

}           

Answer 1:

还有就是你的代码中的错误情侣所以这应该现在的工作。 现在,您可以提取idname值:

      try {
         ArrayList<Stations> stations = new ArrayList<>();

        // Create a JSONObject from the JSON response string
        JSONObject baseJsonResponse = new JSONObject(stationJSON);
        JSONArray stopPointSequenceArrayList = baseJsonResponse.getJSONArray("stopPointSequences");
        if (stopPointSequenceArrayList != null) {
            for (int i = 0; i < stopPointSequenceArrayList.length(); i++) {
                JSONObject elem = stopPointSequenceArrayList.getJSONObject(i);
                if (elem != null) {
                    JSONArray stopPointArrayList = elem.getJSONArray("stopPoint");
                    if (stopPointArrayList != null) {
                        for (int j = 0; j < stopPointArrayList.length(); j++) {
                            JSONObject innerElem = stopPointArrayList.getJSONObject(i);
                            if (innerElem != null) {
                                String id = innerElem.getString("id");
                                String name = innerElem.getString("name");
                                Log.d("Element", name);
                                Log.d("Element", id);
                                Stations station = new Stations(id, name);
                                stations.add(station);
                            }
                        }
                    }
                }
            }
         return stations;
        }
     return null; //something went wrong

    } catch (Exception e) {
        // If an error is thrown when executing any of the above statements in the "try" block,
        // catch the exception here, so the app doesn't crash. Print a log message
        // with the message from the exception.
        Log.e("QueryUtils", "Problem parsing stations JSON results", e);
        return null; // something went wrong exception is thrown

    }


文章来源: How do you return the contents of the following nested JSON Array in Android?