0
for(int i = 0; i < ja.length(); i++){
            JSONObject jo = ja.getJSONObject(i);
            Resources.show(jo);
}

In the above code I want to fetch the json object from json array. But I'm getting an error

org.json.JSONException: JSONArray[0] is not a JSONObject.
at org.json.JSONArray.getJSONObject(JSONArray.java:343)
at webutilities.Try.main(JSON.java:50)

BUILD SUCCESSFUL (total time: 0 seconds)

What else are the methods to fetch a json object from json array.

  • Could you also share what your JSONArray looks like? Seems like you did not store a JSONObject in index zero judging from your exception – T A Nov 15 '18 at 12:34
  • Can you please share the whole code? – GauravRai1512 Nov 15 '18 at 12:35
  • In your example you show an individual element of the array, what type are the elements? The array itself is already a JSON object. – SPlatten Nov 15 '18 at 12:49
  • Refer to the post for clarification. [link](https://stackoverflow.com/questions/1568762/accessing-members-of-items-in-a-jsonarray-with-java) – Yuvi Jum Nov 15 '18 at 12:59

1 Answers1

0

Try

System.out.println(ja.get(i).getClass().toString());

to find out what kind of object is within your JSONArray.

E.g. if you have Strings in there, simply use:

ja.getString(i)