使用下面的代碼來解析json對象內的json數組。解析json數組時出現超出範圍錯誤的索引
JSONObject notificationResponse = new JSONObject(response.toString());
JSONObject value = new JSONObject(notificationResponse.getString("value"));
JSONArray details = value.optJSONArray("details");
final int numberOfItems = details.length();
for (int i =0; i <= numberOfItems; i++){
JSONObject jsonObjects = details.getJSONObject(i);
String text = jsonObjects.getString("text");
String date = jsonObjects.getString("date & time");
Log.d(TAG,text + " " +date);
}
} catch (Exception e) {
e.printStackTrace();
}
以下是我的json。
{"status":1,"value":{"details":[{"text":"sample", "date & time":"12-NOV-15 3:30pm"}]}}
我的代碼out of range
例外在JSONObject jsonObjects = details.getJSONObject(i);
只需更換for循環<=與<僅 – Manifest
除去等號條件 我