标签:
private List<Dialog_forlist> jSON2List2(String jsonPath) throws JSONException {
List<Dialog_forlist> list = new ArrayList<>();
InputStream is = AssetManagerUtils.getData(jsonPath, ReleaseActivity.this);
String str = StreamTools.readStream(is);
JSONObject object = new JSONObject(str);
JSONArray jsonArray = object.getJSONArray("city");
JSONObject jsonObject = jsonArray.getJSONObject(0);
JSONArray jsonArray1 = jsonObject.getJSONArray("tdistrict");
for (int i = 0; i < jsonArray1.length(); i++) {
Dialog_forlist dialog_forlist = new Dialog_forlist();
List<String> list_city = new ArrayList<>();
if (i == 0) {
JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
dialog_forlist.setName(jsonObject1.getString("name"));
list_city.add("不限");
dialog_forlist.setList(list_city);
} else {
JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
dialog_forlist.setName(jsonObject1.getString("name"));
JSONArray jsonArray2 = jsonObject1.getJSONArray("bussinessareaList");
for (int j = 0; j < jsonArray2.length(); j++) {
JSONObject jsonObject2 = jsonArray2.getJSONObject(i);
list_city.add(jsonObject2.getString("name"));
}
dialog_forlist.setList(list_city);
}
list.add(dialog_forlist);
}
return list;
}
这是一个很常犯的错误,如果不是当时记得了,那就很难再发现(看代码是很难发现)。
用了debug才发现错误的所在,竟然是将j写成了i!!!
for (int j = 0; j < jsonArray2.length(); j++) {
JSONObject jsonObject2 = jsonArray2.getJSONObject(i);
list_city.add(jsonObject2.getString("name"));
}
标签:
原文地址:http://www.cnblogs.com/SkyWindy/p/4673726.html