我的问题是当我将这两个数组设置在一个列表中时,我无法获得这两个数组的正确值。
下面是我从服务器获得的 json 响应。
User_Label_List
并不总是得到,但Admin_Label_List
数组很常见。
{
"User_Label_List": [
{
"iLabelID": "60",
"vLabel": "At Ground"
}
],
"Admin_Label_List": [
{
"iLabelID": "53",
"vLabel": "At School"
},
{
"iLabelID": "51",
"vLabel": "At Office"
},
{
"iLabelID": "52",
"vLabel": "At Work"
},
{
"iLabelID": "50",
"vLabel": "At None"
},
{
"iLabelID": "49",
"vLabel": "At Home"
}
]
}
这是我的代码。
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("Admin_Label_List");
labelarray = new ArrayList<LabelModel>();
for (int i = 0; i < jsonArray.length(); i++) {
labelModel = new LabelModel();
JSONObject labeljson = jsonArray.getJSONObject(i);
// lableList.put("iLabelID", labeljson.getString("iLabelID"));
// lableList.put("vLabel", labeljson.getString("vLabel"));
labelModel.setLabelId(labeljson.getString("iLabelID"));
labelModel.setLabelName(labeljson.getString("vLabel"));
labelarray.add(labelModel);
}
Log.e("length", "== "+jsonObject.length());
if (jsonObject.length() == 2) {
String arryName = jsonObject.names().getString(1);
if (arryName.equalsIgnoreCase("User_Label_List")) {
JSONArray userjsonArray = jsonObject.getJSONArray("User_Label_List");
userlabelarray = new ArrayList<LabelModel>();
for (int i = 0; i < userjsonArray.length(); i++) {
// userLabelModel = new UserLabelModel();
JSONObject labeljson = userjsonArray.getJSONObject(i);
// lableList.put("iLabelID", labeljson.getString("iLabelID"));
// lableList.put("vLabel", labeljson.getString("vLabel"));
labelModel.setLabelId(labeljson.getString("iLabelID"));
labelModel.setLabelName(labeljson.getString("vLabel"));
userlabelarray.add(labelModel);
}
// userlabelarray.addAll(labelarray);
}
List<LabelModel> union = new ArrayList<LabelModel>(labelarray);
union.addAll(userlabelarray);
customeLabelAdpater = new CustomeLabelAdpater(ChooselabelActivity.this, lableList);
listView.setAdapter(customeLabelAdpater);
}
那么有人可以建议我如何实现这一目标吗?