Reputation: 1698
I've got an JSON array, and I would like to select the data from it. I would like to get all subjects, but I don't know how to do it.
Code:
JSONObject jsonObject = new JSONObject(thatarray);
JSONArray jsonArray = jsonObject.getJSONArray("response");
int arrSize = jsonArray.length();
List<Integer> sub = new ArrayList<Integer>(arrSize);
for (int i = 0; i < arrSize; ++i) {
jsonObject = jsonArray.getJSONObject(i);
System.out.println("Output: " + jsonObject.toString());
}
Upvotes: 0
Views: 6069
Reputation: 27501
Actually "response" is jsonObject and "data" is jsonArray.. u can differentiate between jsonArray and jsonObject by viewing {} and []... hope it will help :)
I tried below code myself on your JSON and it is working.
try {
JSONObject jsonObject = new JSONObject(thatarray);
jsonObject = jsonObject.getJSONObject("response");
JSONArray jsonArray = jsonObject.getJSONArray("data");
JSONArray jsonArraysubject;
for (int i = 0; i < jsonArray.length() - 1; i++) {
jsonObject = jsonArray.getJSONObject(i);
jsonArraysubject = jsonObject.getJSONArray("subjects");
Log.d("MyLog", jsonArraysubject + "");
}
} catch (JSONException e) {
e.printStackTrace();
}
Upvotes: 3