如何解析JSON嵌套对象?

他们下去

谁能告诉我如何解析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”的方式,因此我可以循环浏览每个stop_id。关于最佳方法的任何想法吗?

编辑:对不起,我的帖子的其余部分必须在我提交之前删除,现在添加其余部分:

一旦下载了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();
                }

如何将“停靠点”设置为对象?还是它是一个ObjectArray?这样我就可以遍历每个值并获取每个stop_id?

马龙·帕特里克(Marlon Patrick)

试试这个:

    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"));
        }
    }

此代码显示数组的每个元素的stop_id 3734和2750。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章