那一个实例做背景:
String str="{"msg":"用户发布的求租信息","status":"Y","error":null,"data":[{"checkInTimeStr":"2015-02-02","title","好好好"},{"checkInTimeStr":"2015-02-02","title","好好好"}]";
try {
JSONObject object = new JSONObject(str);
Log.e("object", "object" + object.toString());
if (object.getString("status").equals("Y")) {
JSONArray jsonArray = object.getJSONArray("data");
list_for_release = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
list_for_release.add(Integer.toString(jsonObject.getInt("id")));
Map<String, String> map_for_list = new HashMap<String, String>();
map_for_list.put("createTimeStr", jsonObject.getString("createTimeStr"));
map_for_list.put("title", jsonObject.getString("title"));
map_for_list.put("type", "求租");
getList_map_release.add(map_for_list);
Log.e("长度是", "成都市" + getList_map_release.size());
Log.e("LLLLLLLL", "LLLLLLLL" + getList_map_release.toString());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
红色字体的地方是没有执行到的代码段。
为什么会执行不下去了呢?
那是因为jsonObject中没有“createTimeStr”这个字段,那么程序就会不再解析,那么下面的代码段就没法解析到了。