Java Noobie Java Noobie - 6 months ago 33
JSON Question

Get JSON array from JSON array Java android

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. I searched a lot on google (2/3 days now) and just couldn't fix it.

My Array output:
Array

My Java code: (thatarray = my array listed above)


JSONObject o = new JSONObject(thatarray);
JSONArray a = o.getJSONArray("response");
int arrSize = a.length();
List<Integer> sub = new ArrayList<Integer>(arrSize);
for (int i = 0; i < arrSize; ++i) {
o = a.getJSONObject(i);
System.out.println("Output: " + o.toString());
}
}catch (org.json.JSONException e) {e.printStackTrace();}


I use this to get the response:


JSONArray a = o.getJSONArray("response");


but when I want to get the subjects then, it just won't work. How can I fix this?

Answer

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 = jsonObject.getJSONArray("data");

        JSONArray jsonArraysunject  ;
        for(int i=0;i<jsonArray.length()-1;i++){
            jsonObject = jsonArray.getJSONObject(i);
            jsonArraysunject = jsonObject.getJSONArray("subjects");

            Log.d("MyLog",jsonArraysunject+"");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
Comments