Is it possible to iterate through JSONArray?

Java

Java Problem Overview


> Possible Duplicates:
> JSON Array iteration in Android/Java
> JSONArray with Java

Is it possible to iterate through JSONArray object using Iterator?

Java Solutions


Solution 1 - Java

Not with an iterator.

For org.json.JSONArray, you can do:

for (int i = 0; i < arr.length(); i++) {
  arr.getJSONObject(i);
}

For javax.json.JsonArray, you can do:

for (int i = 0; i < arr.size(); i++) {
  arr.getJsonObject(i);
}

Solution 2 - Java

You can use the opt(int) method and use a classical for loop.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEugeneView Question on Stackoverflow
Solution 1 - JavaBen McCannView Answer on Stackoverflow
Solution 2 - JavaJean HominalView Answer on Stackoverflow