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.
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);
}
How to print the Json Array using for each loop
java - JSON - Iterate through JSONArray - Stack Overflow
How to iterate this JSON Array using Java and org.json in Android? - Stack Overflow
Java loop over Json array? - Stack Overflow
Videos
Change
JSONObject objects = getArray.getJSONArray(i);
to
JSONObject objects = getArray.getJSONObject(i);
or to
JSONObject objects = getArray.optJSONObject(i);
depending on which JSON-to/from-Java library you're using. (It looks like getJSONObject will work for you.)
Then, to access the string elements in the "objects" JSONObject, get them out by element name.
String a = objects.get("A");
If you need the names of the elements in the JSONObject, you can use the static utility method JSONObject.getNames(JSONObject) to do so.
String[] elementNames = JSONObject.getNames(objects);
"Get the value for the first element and the value for the last element."
If "element" is referring to the component in the array, note that the first component is at index 0, and the last component is at index getArray.length() - 1.
I want to iterate though the objects in the array and get thier component and thier value. In my example the first object has 3 components, the scond has 5 and the third has 4 components. I want iterate though each of them and get thier component name and value.
The following code does exactly that.
import org.json.JSONArray;
import org.json.JSONObject;
public class Foo
{
public static void main(String[] args) throws Exception
{
String jsonInput = "{\"JObjects\":{\"JArray1\":[{\"A\":\"a\",\"B\":\"b\",\"C\":\"c\"},{\"A\":\"a1\",\"B\":\"b2\",\"C\":\"c3\",\"D\":\"d4\",\"E\":\"e5\"},{\"A\":\"aa\",\"B\":\"bb\",\"C\":\"cc\",\"D\":\"dd\"}]}}";
// "I want to iterate though the objects in the array..."
JSONObject outerObject = new JSONObject(jsonInput);
JSONObject innerObject = outerObject.getJSONObject("JObjects");
JSONArray jsonArray = innerObject.getJSONArray("JArray1");
for (int i = 0, size = jsonArray.length(); i < size; i++)
{
JSONObject objectInArray = jsonArray.getJSONObject(i);
// "...and get thier component and thier value."
String[] elementNames = JSONObject.getNames(objectInArray);
System.out.printf("%d ELEMENTS IN CURRENT OBJECT:\n", elementNames.length);
for (String elementName : elementNames)
{
String value = objectInArray.getString(elementName);
System.out.printf("name=%s, value=%s\n", elementName, value);
}
System.out.println();
}
}
}
/*
OUTPUT:
3 ELEMENTS IN CURRENT OBJECT:
name=A, value=a
name=B, value=b
name=C, value=c
5 ELEMENTS IN CURRENT OBJECT:
name=D, value=d4
name=E, value=e5
name=A, value=a1
name=B, value=b2
name=C, value=c3
4 ELEMENTS IN CURRENT OBJECT:
name=D, value=dd
name=A, value=aa
name=B, value=bb
name=C, value=cc
*/
for (int i = 0; i < getArray.length(); i++) {
JSONObject objects = getArray.getJSONObject(i);
Iterator key = objects.keys();
while (key.hasNext()) {
String k = key.next().toString();
System.out.println("Key : " + k + ", value : "
+ objects.getString(k));
}
// System.out.println(objects.toString());
System.out.println("-----------");
}
Hope this helps someone
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");
}
In your code the element dataArray is an array of JSON objects, not a JSON object itself. The elements A, B, and C are part of the JSON objects inside the dataArray JSON array.
You need to iterate over the array
public static void main(String[] args) throws Exception {
String jsonStr = "{ \"dataArray\": [{ \"A\": \"a\", \"B\": \"b\", \"C\": \"c\" }, { \"A\": \"a1\", \"B\": \"b2\", \"C\": \"c3\" }] }";
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray c = jsonObj.getJSONArray("dataArray");
for (int i = 0 ; i < c.length(); i++) {
JSONObject obj = c.getJSONObject(i);
String A = obj.getString("A");
String B = obj.getString("B");
String C = obj.getString("C");
System.out.println(A + " " + B + " " + C);
}
}
prints
a b c
a1 b2 c3
I don't know where msg is coming from in your code snippet.
Java Docs to the rescue:
You can use http://www.json.org/javadoc/org/json/JSONObject.html#getJSONArray(java.lang.String) instead
JSONArray dataArray= sync_reponse.getJSONArray("dataArray");
for(int n = 0; n < dataArray.length(); n++)
{
JSONObject object = dataArray.getJSONObject(n);
// do some stuff....
}
I think this code is short and clear:
int id;
String name;
JSONArray array = new JSONArray(string_of_json_array);
for (int i = 0; i < array.length(); i++) {
JSONObject row = array.getJSONObject(i);
id = row.getInt("id");
name = row.getString("name");
}
Is that what you were looking for?
I have done it two different ways,
1.) make a Map
HashMap<String, String> applicationSettings = new HashMap<String,String>();
for(int i=0; i<settings.length(); i++){
String value = settings.getJSONObject(i).getString("value");
String name = settings.getJSONObject(i).getString("name");
applicationSettings.put(name, value);
}
2.) make a JSONArray of names
JSONArray names = json.names();
JSONArray values = json.toJSONArray(names);
for(int i=0; i<values.length(); i++){
if (names.getString(i).equals("description")){
setDescription(values.getString(i));
}
else if (names.getString(i).equals("expiryDate")){
String dateString = values.getString(i);
setExpiryDate(stringToDateHelper(dateString));
}
else if (names.getString(i).equals("id")){
setId(values.getLong(i));
}
else if (names.getString(i).equals("offerCode")){
setOfferCode(values.getString(i));
}
else if (names.getString(i).equals("startDate")){
String dateString = values.getString(i);
setStartDate(stringToDateHelper(dateString));
}
else if (names.getString(i).equals("title")){
setTitle(values.getString(i));
}
}
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"