这是我的json

{
    "2017": {
        "11": {
            "8": {
                "status": ""
            },
            "10": {
                "status": ""
            },
            "24": {
                "status": ""
            },
            "present": 0,
            "absent": 0
        },
        "12": {
            "1": {
                "status": ""
            },
            "2": {
                "status": ""
            },
            "present": 0,
            "absent": 0
        }
    }
}

在我上面的json中,2017是年份,11是月份,8是日期。
我一个月也拿不到12英镑。我要到11个月才拿到,日期倒过来了
这就是我从json获取数据时得到的结果
12-03 10:26:23.592 18581-18581/?d /年:2017
12-03 10:26:23.592 18581-18581/?月/月:11
12-03 10:26:23.592 18581-18581/?D/16:24
12-03 10:26:23.592 18581-18581/?D /标题:A
12-03 10:26:23.592 18581-18581/?D/16:10
12-03 10:26:23.592 18581-18581/?D /标题:A
12-03 10:26:23.592 18581-18581/?D/16:8
12-03 10:26:23.592 18581-18581/?D /标题:P
这是我写的代码
try {
            JSONObject object = new JSONObject(response);
            Iterator iterator = object.keys();
            attendance_pojo pojo= new attendance_pojo();

            while (iterator.hasNext()) {
                String year = (String) iterator.next();
                pojo.setYear(year);

                Log.d("year", year);
                JSONObject obj = object.getJSONObject(year);
                Iterator iterator2 = obj.keys();


                while (iterator2.hasNext()) {

                    String month = (String) iterator2.next();
                    pojo.setMonth(month);
                    Log.d("month", month);
                    JSONObject ob = obj.getJSONObject(month);

                    Iterator iterator3 = ob.keys();

                        int datecntr=0;
                    while (iterator3.hasNext()) {
                        datecntr++;
                        String date = (String) iterator3.next();
                        pojo.setDate(date);
                        JSONObject ob1 = ob.getJSONObject(date);
                        Log.d("16", date);
                        String title = ob1.getString("status");
                        Log.d("title", title);
                    }
                    pojo.setDatecounter(datecntr);

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

最佳答案

1)试图在包含两个对象的对象上迭代。您可能希望将数组中的月份定义为:
2)返回的JSON元素的顺序是随机的,它们不是按照结构的特定顺序来的。

{
    "2017": [
        "11": {
            "8": {
                "status": ""
            },
            "10": {
                "status": ""
            },
            "24": {
                "status": ""
            },
            "present": 0,
            "absent": 0
        },
        "12": {
            "1": {
                "status": ""
            },
            "2": {
                "status": ""
            },
            "present": 0,
            "absent": 0
        }
    ]
}

08-04 01:49