如何解析JSON嵌套对象?

时间:2015-03-14 23:20:27

标签: java android json parsing

任何人都可以告诉我如何解析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,例如,似乎无法找到如何将对象设置为"停止"所以我可以遍历每一个。有关最佳方法的任何想法吗?

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

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

如何设置&#34;停止&#34;作为对象?还是ObjectArray?这样我就可以遍历每个值并获得每个stop_id?

2 个答案:

答案 0 :(得分:1)

试试这个:

    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。

答案 1 :(得分:0)

想出来了!

感谢大家的帮助。

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