Reputation: 373
Here is my Json
{
"2017": {
"11": {
"8": {
"status": ""
},
"10": {
"status": ""
},
"24": {
"status": ""
},
"present": 0,
"absent": 0
},
"12": {
"1": {
"status": ""
},
"2": {
"status": ""
},
"present": 0,
"absent": 0
}
}
}
In my above JSON 2017 is the year, 11 is the month, and 8 is the date . I am not able to get 12 as month. I am just getting it till 11th month, and dates are coming in reverse order This is what I am getting When I am fetching the data from json
12-03 10:26:23.592 18581-18581/? D/year: 2017
12-03 10:26:23.592 18581-18581/? D/month: 11
12-03 10:26:23.592 18581-18581/? D/16: 24
12-03 10:26:23.592 18581-18581/? D/title: A
12-03 10:26:23.592 18581-18581/? D/16: 10
12-03 10:26:23.592 18581-18581/? D/title: A
12-03 10:26:23.592 18581-18581/? D/16: 8
12-03 10:26:23.592 18581-18581/? D/title: P
and here is the code I has written to fetch this
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();
}}
Upvotes: 3
Views: 61
Reputation: 2065
1)You are trying to iterate over an object containing two objects. You probably want to define the months within an array as below:
2) The order of the json elements returned are random, they do not come in the specific order of the structure.
{
"2017": [
"11": {
"8": {
"status": ""
},
"10": {
"status": ""
},
"24": {
"status": ""
},
"present": 0,
"absent": 0
},
"12": {
"1": {
"status": ""
},
"2": {
"status": ""
},
"present": 0,
"absent": 0
}
]
}
Upvotes: 1