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 can do something like
for(var k in result) {
console.log(k, result[k]);
}
which loops over all the keys in the returned json and prints the values. However, if you have a nested structure, you will need to use
typeof result[k] === "object"
to determine if you have to loop over the nested objects. Most APIs I have used, the developers know the structure of what is being returned, so this is unnecessary. However, I suppose it's possible that this expectation is not good for all cases.
Try this:
$.each(result,function(index, value){
console.log('My array has at position ' + index + ', this value: ' + value);
});
How to loop through an array with JSON objects
How to print the Json Array using for each loop
java - foreach on json array - Stack Overflow
How do I iterate over a JSON structure?
Videos
Hi all,
Im really struggling with this problem today. So basically, I have an array in the format
arr = [{title: " some title", id: "some id"}, {title: " some title2", id: "some id2"}] and all im trying to do is loop through each item in the array and get the value of the ids.
Here is what ive tried:
for( var i = 0; i< arr.length; i++){
console.log(arr[i].id)
}
It keeps showing up as undefined, please can anyone assist me? I would like the result to be "some id"
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);
}
var arr = [ {"id":"10", "class": "child-of-9"}, {"id":"11", "class": "child-of-10"}];
for (var i = 0; i < arr.length; i++){
document.write("<br><br>array index: " + i);
var obj = arr[i];
for (var key in obj){
var value = obj[key];
document.write("<br> - " + key + ": " + value);
}
}
note: the for-in method is cool for simple objects. Not very smart to use with DOM object.
Taken from jQuery docs:
var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one:1, two:2, three:3, four:4, five:5 };
jQuery.each(arr, function() {
$("#" + this).text("My id is " + this + ".");
return (this != "four"); // will stop running to skip "five"
});
jQuery.each(obj, function(i, val) {
$("#" + i).append(document.createTextNode(" - " + val));
});