【发布时间】:2017-12-04 10:29:13
【问题描述】:
我的 Json 2017 是年份,11 是月份,下一个是日期
Well this is my json
{
"2017": {
"11": {
"8": {
"status": "P"
},
"10": {
"status": "A"
},
"24": {
"status": "A"
}
},
"12": {
"1": {
"status": "P"
},
"2": {
"status": "A"
}
}
}
}
这里我从 Json 中获取值并存储在一些变量中
try {
JSONObject object = new JSONObject(response);
Iterator iterator = object.keys();
attendance_pojo pojo= new attendance_pojo();
while (iterator.hasNext()) {
String year = (String) iterator.next();
/*Log.d("year", year);*/
JSONObject obj = object.getJSONObject(year);
Iterator iterator2 = obj.keys();
while (iterator2.hasNext()) {
String month = (String) iterator2.next();
/*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.setYear(year);
pojo.setMonth(month);
pojo.setDate(date);
JSONObject ob1 = ob.getJSONObject(date);
/*Log.d("16", date);*/
String status = ob1.getString("status");
if (status.equals("P"))
{
present++;
}
else
absent++;
/* Log.d("title", status);*/
}
pojo.setDatecounter(datecntr);
Log.d("counter",""+datecntr);
Log.d("kuqw",present+" "+absent);
pojo.setPresent(""+present);
pojo.setAbsent(""+absent);
present=0;
absent=0;
arrayList.add(pojo);
adapter.notifyDataSetChanged();
}
}
我在这里从 pojo 检索值,以便我可以在一些文本视图中设置它们
attendance_pojo pojo= arrayList.get(position);
String months= pojo.getMonth();
Log.d("months",months);
months= checkmonth(months);
Log.d("datess",""+pojo.getDate());
month.setText(months+" "+pojo.getYear());
workdays.setText(""+pojo.getDatecounter());
But i am always getting 12 when I am getting Log of datess
12-04 15:27:25.220 2523-2523/rock.school_hub D/months: 12
12-04 15:27:25.226 2523-2523/rock.school_hub D/months: 12
12-04 15:27:25.229 2523-2523/rock.school_hub D/months: 12
12-04 15:27:25.236 2523-2523/rock.school_hub D/months: 12
根据我的 json 首先我应该得到 11 然后 12,但在可变月份,它总是显示 12
【问题讨论】:
-
显然这是因为您在错误的位置创建了新的 pojo 实例(实际上您的 arrayList 包含多次添加的 pojo 的相同实例)
-
你能告诉我如何解决它。
-
在要添加的范围内创建新的 pojo 实例...
-
是的,谢谢..它对我有用..!!
标签: android json arraylist pojo