如何解析 JSON 嵌套对象?

How to parse JSON nested objects?

任何人都可以告诉我如何解析 JSON 嵌套对象以便我可以访问每个对象的值吗?我只能解析根对象...

[
{
    "direction_id": "0",
    "stops": [
        {
            "stop_id": "3734",
            "stop_code": "5266",
            "stop_name": "WB @78 AV TERMINAL",
            "stop_desc": "",
            "stop_lat": 51.122802,
            "stop_lon": -114.070807,
            "zone_id": "",
            "stop_url": "",
            "location_type": "",
            "parent_station": "",
            "agency_key": "calgary-transit",
            "loc": [
                -114.070807,
                51.122802
            ],
            "_id": "54ff06c07ec9b14c0412a602"
        },
        {
            "stop_id": "2750",
            "stop_code": "5268",
            "stop_name": "EB 78 AV@HUNTHAM RD NE",
            "stop_desc": "78 AV NE & HUNTHAM RD NE",
            "stop_lat": 51.121726,
            "stop_lon": -114.066023,
            "zone_id": "",
            "stop_url": "",
            "location_type": "",
            "parent_station": "",
            "agency_key": "calgary-transit",
            "loc": [
                -114.066023,
                51.121726
            ],
            "_id": "54ff06c07ec9b14c0412a280"
        },
        {
            "stop_id": "2751",
            "stop_code": "9010",
            "stop_name": "EB 78 AV@HUNTRDG HL NE",
            "stop_desc": "78 AV NE & HUNTRIDGE HL NE",
            "stop_lat": 51.121179,
            "stop_lon": -114.061799,
            "zone_id": "",
            "stop_url": "",
            "location_type": "",
            "parent_station": "",
            "agency_key": "calgary-transit",
            "loc": [
                -114.061799,
                51.121179
            ],
            "_id": "54ff06c07ec9b14c0412a281"
        },
        {
            "stop_id": "2752",
            "stop_code": "9011",
            "stop_name": "EB 78 AV@HUNTINGTON RD NE",
            "stop_desc": "78 AV NE & HUNTINGTON RD NE",
            "stop_lat": 51.12102,
            "stop_lon": -114.058805,
            "zone_id": "",
            "stop_url": "",
            "location_type": "",
            "parent_station": "",
            "agency_key": "calgary-transit",
            "loc": [
                -114.058805,
                51.12102
            ],
            "_id": "54ff06c07ec9b14c0412a282"
        },

所以我试图解析每个 stop_id,例如,似乎无法找到如何将对象设置为 "stops" 以便循环遍历每个对象。关于执行此操作的最佳方法有什么想法吗?

编辑:抱歉,我的 post 的其余部分在我提交之前一定已被删除,现在添加其余部分:

一旦我下载了 JSON (json) 我尝试像这样解析它,日志只是一行,其中的所有数据都是一个...

            for(int n = 0; n < json.length(); n++)
            {
                try {
                    JSONObject object = json.getJSONObject(n);

                    String short_name = object.getString("stops");



                    Log.v("PARSE",short_name);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

如何将 "stops" 设置为对象?或者它是一个 ObjectArray??这样我就可以遍历每个值并获得每个 stop_id?

试试这个:

    public static void main(String[] args) throws JSONException {
        JSONObject j = new JSONObject("{\"stops\": [{\"stop_id\": \"3734\",\"stop_code\": \"5266\"},{\"stop_id\": \"2750\",\"stop_code\": \"5268\"}]}");        
        JSONArray array = j.getJSONArray("stops");
        for (int i = 0; i < array.length(); i++) {
            System.out.println(array.getJSONObject(i).getString("stop_id"));
        }
    }

此代码打印 3734 和 2750,即数组每个元素的 stop_id。

想通了!

感谢大家的帮助。

  for(int i=0;i<json.length();i++){

            try {

                JSONObject stops =json.getJSONObject(i);
                JSONArray array = stops.getJSONArray("stops");

                for (int st = 0; st < array.length(); st++) {

JSONObject currentStop = array.getJSONObject(st);
                    String stop_desc = currentStop.getString("stop_name");
                    String stop_id = currentStop.getString("stop_id");
                    String stop_code = currentStop.getString("stop_code");

           }

            } catch (JSONException e) {
                e.printStackTrace();
            }