Seems like you can't iterate through JSONArray with a for each. You can loop through your JSONArray like this:
for (int i=0; i < arr.length(); i++) {
arr.getJSONObject(i);
}
Source
Answer from dguay on Stack OverflowSeems like you can't iterate through JSONArray with a for each. You can loop through your JSONArray like this:
for (int i=0; i < arr.length(); i++) {
arr.getJSONObject(i);
}
Source
Apparently, org.json.simple.JSONArray implements a raw Iterator. This means that each element is considered to be an Object. You can try to cast:
for(Object o: arr){
if ( o instanceof JSONObject ) {
parse((JSONObject)o);
}
}
This is how things were done back in Java 1.4 and earlier.
You want something like this..
JSONArray jsonArray = new JSONArray(sJSON);
JSONArray jsonPersonData = jsonArray.getJSONArray(1);
for (int i=0; i<jsonPersonData.length(); i++) {
JSONObject item = jsonPersonData.getJSONObject(i);
String name = item.getString("name");
String surname = item.getString("surname");
}
You shoul not use JSONObject.toString. This is how you should iterate your array:
for(int i=0;i<arrJSON.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
String name =json_data.getString("name");
String surname =json_data.getString("surname");
}
Videos
Java "foreach" syntax is not applicable because the JSONArray API does not implement the Iterable interface.
Can we import any jar so that it would work?
The answer to that is No. The org.json APIs are "baked in" for Android. I don't think they can be changed without modifying / replacing the Android runtime libraries.
The solution is to either petition "someone"1 to change the official org.json APIs in Android.
Or use a different JSON API, which could be problematic depending on the other APIs that you are using.
Or just bite the bullet and use indexing. It is not that bad ...
1 - Good luck with that! For a start, it is not clear who you should petition ...
JSONArray is only iterable starting from JSON-java 20150729. You need to update your version.
I bet the libraries you are using are different:
For library
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
the JSONArray doesn't implement iterator, thus you need for-loop like below:
for (int i = 0; i < logList.length(); i++) {
//your operation
}
If you are using below library
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
Then use below approach:
for (Object jsonObject : logList) {
if(jsonObject instanceof JSONObject)
{
//your operation
}
}
If org.json.JSONArray is the JSONArray you are refering.
https://developer.android.com/reference/org/json/JSONArray
JSONArray extends Object. For each loop, as the exception suggest, only support Iterable or array. Iterable is an interface. JSONArray do not implement that. JSONArray clearly is not a array. So neither case fail, thus the error shows.
If you need to loop through its element, you could
for (int i = 0; i < logList.length(); i++) {
Object log = logList.get(i);
}