Unfortunately, JsonArray does not expose an iterator. So you will have to iterate through it using an index range:

for (i in 0 until persons.length()) {
    val item = persons.getJSONObject(i)

    // Your code here
}
Answer from 0x60 on Stack Overflow
🌐
Baeldung
baeldung.com › home › kotlin › kotlin collections › iterate through a jsonarray in kotlin
Iterate Through a JSONArray in Kotlin | Baeldung on Kotlin
September 7, 2024 - To summarise, JSONArray does not expose an iterator. As shown above, one option is to use for-loops.
🌐
Android Developers
developer.android.com › api reference › jsonarray
JSONArray | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
TutorialsPoint
tutorialspoint.com › how-to-iterate-a-json-array-in-android-using-kotlin
How to iterate a JSON Array in Android using Kotlin?
{ super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" textView = findViewById(R.id.textView) val data = StringBuilder() val jsonObject = JSONObject(strJson) val jsonArray = jsonObject.optJSONArray("Employee") for (i in 0 until jsonArray.length()) { val jsonObject = jsonArray.getJSONObject(i) val id = jsonObject.optString("ID").toInt() val name = jsonObject.optString("Name") val salary = jsonObject.optString("Salary").toFloat() data.append("Employee ").append(i).append(" : ID= ") .append(id).append(" " + "Name= ") .append(name).append(" Salary= ") .append(salary).append(" ") } textView.text = data.toString() } } Step 4 − Add the following code to androidManifest.xml ·
🌐
GitHub
github.com › android › android-ktx › issues › 241
JSONObject & JSONArray extensions · Issue #241 · android/android-ktx
February 7, 2018 - T) } } inline fun <reified T> JSONArray.forEachNotNull(action: (T) -> Unit) { (0..length()).forEach { action(get(it) as T) } } // Summary inline fun <reified T: Comparable<T>> JSONArray.max() = toList<T>().filterNotNull().max() inline fun <reified T: Comparable<T>> JSONArray.min() = toList<T>().filterNotNull().min() ...
Author   ethanblake4
🌐
Kotlin Discussions
discuss.kotlinlang.org › javascript
Iterating over Json properties - JavaScript
August 29, 2016 - Hi there! I’m unsuccessfully trying to find a way to iterate over a Json object (which is expected to only contain properties with String values) so that I can fill a Map<String,String> from it. In either way I end up w…
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.collections › for-each.html
forEach
Kotlin is a concise and multiplatform programming language by JetBrains. Enjoy coding and build server-side, mobile, web, and desktop applications efficiently.
Find elsewhere
🌐
GitHub
github.com › kittinunf › GithubKotlin › blob › master › app › src › main › kotlin › com › github › kittinunf › githubkotlin › extension › JSONArrays.kt
GithubKotlin/app/src/main/kotlin/com/github/kittinunf/githubkotlin/extension/JSONArrays.kt at master · kittinunf/GithubKotlin
August 29, 2015 - import org.json.JSONArray · · /** * Created by Kittinun Vantasin on 8/29/15. */ · public fun JSONArray.asSequence(): Sequence<Any> { return object : Sequence<Any> { · override fun iterator() = object : Iterator<Any> { · val it = (0..this@asSequence.length() - 1).iterator() ·
Author   kittinunf
🌐
Kotlin Discussions
discuss.kotlinlang.org › javascript
Arraylist from json can not be iterated - JavaScript
August 12, 2017 - i created a ArrayList with PersonDto objects and transform it to a json string on the server to send it to the client. in the client i try to parse this string back to the ArrayList of PersonDto objects but only get an Array back val listPerson = JSON.parse (data...
🌐
Utecq
customer-analysis.utecq.dev › gij0t › 6r7i.php
Jsonarray foreach kotlin
In this tutorial, we will present examples to iterate over the Array us ing looping statements and forEach() method. © 2021 Copyright Generated by dokka Generated by dokka Sep 29, 2021 · JSON array are ordered list of values. Hence, we can’t use the typical for-each pattern to iterate a JSONArray. Oct 23, 2018 · hello, Bellow is My Web Service written in C#. json. Let’s look at some other ways which we can use instead. We can iterate through a JSONArray using a for loop: val booksJSONArray = JSONArray Kotlin: Iterate through a JSONArray.
🌐
GitHub
github.com › Kotlin › kotlinx.serialization › issues › 2127
`JsonArrayBuilder` - add convenience function for adding multiple elements · Issue #2127 · Kotlin/kotlinx.serialization
December 17, 2022 - Currently this must be done one at a time, using JsonArrayBuilder.add(value: String?) val alphaDependencies = listOf("a", "b", "c") buildJsonObject { put("name", "alpha") putJsonArray("dependencies") { // currently have to manually define a for-loop alphaDependencies.forEach { dep -> add(dep) } } }.toString()
Author   aSemy
🌐
GeeksforGeeks
geeksforgeeks.org › android-extract-data-from-json-array-using-volley-library-with-kotlin
Android - Extract Data From JSON Array using Volley Library with Kotlin - GeeksforGeeks
July 22, 2022 - 5 min read JSON Parsing in Android Using Volley Library with Kotlin · JSON is a JavaScript object notation which is a format to exchange the data from the server. JSON stores the data in a lightweight format. With the help of JSON, we can access the data in the form of JsonArray, JsonObject, and JsonStringer.
🌐
Baeldung
baeldung.com › home › json › iterating over an instance of org.json.jsonobject
Iterating Over an Instance of org.json.JSONObject | Baeldung
May 5, 2025 - void handleJSONArray(JSONArray jsonArray) { jsonArray.iterator().forEachRemaining(element -> { handleValue(element) }); }
🌐
Webkul
webkul.com › home › how to iterate through jsonobject in java and kotlin
How to Iterate through JSONObject in Java and Kotlin - Webkul Blog
January 16, 2026 - String jsonString = "{\"name\":\"John\", \"age\":30, \"hobbies\":[\"reading\", \"gaming\", \"coding\"]}"; JSONObject mainObject = new JSONObject(jsonString); Iterator<String> mainKeys = mainObject.keys(); while (mainKeys.hasNext()) { String key = mainKeys.next(); Object value = mainObject.get(key); if (value instanceof JSONArray) { JSONArray jsonArray = (JSONArray) value; for (int i = 0; i < jsonArray.length(); i++) { Log.d("JSON", "Array Item: " + jsonArray.get(i)); } } } Kotlin’s smart casting and concise syntax simplify nested JSONArray handling.
🌐
Kotlin
kotlinlang.org › api › kotlinx.serialization › kotlinx-serialization-json › kotlinx.serialization.json › -json-array
JsonArray | kotlinx.serialization – Kotlin Programming Language
kotlinx-serialization-json/kotlinx.serialization.json/JsonArray · @Serializable(with = JsonArraySerializer::class · )class JsonArray(content: List<JsonElement>) : JsonElement, List<JsonElement> (source) Class representing JSON array, consisting of indexed values, where value is arbitrary JsonElement ·
🌐
TutorialsPoint
tutorialspoint.com › how-to-iterate-a-json-array-in-android
How to iterate a JSON Array in Android?
This example demonstrates how do I iterate JSON Array in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step