You need to first get a hold of the array nested in your object with the "items" key, and then you can iterate over that array. The basic way to do this (if you're sure that it only contains strings) would be to loop over its indexes and call getString on the array for each index:

val my_json = result.get().obj()
val items = my_json.getJSONArray("items")

for (i in 0 until items.length()) {
    val item = items.getString(i)
    // use item
}

If you don't want to deal with indexes while iterating, you could wrap the iteration of a JSONArray into an extension function:

fun JSONArray.forEachString(action: (String) -> Unit) {
    for (i in 0 until length()) {
        action(getString(i))
    }
}

Which could then be used like this:

val items = my_json.getJSONArray("items")
items.forEachString { item ->
    // use item
}

You could extend the JSONArray class with an iterator function as well if you really wanted to iterate the array with a regular for loop, but it would be more trouble than it's worth.

Answer from zsmb13 on Stack Overflow
🌐
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, \"city\":\"New York\"}"; JSONObject jsonObject = new JSONObject(jsonString); Iterator<String> keys = jsonObject.keys(); while (keys.hasNext()) { String key = keys.next(); Object value = jsonObject.get(key); Log.d("JSON", "Key: " + key + ", Value: " + value); } Kotlin simplifies JSON iteration with more concise syntax and better type safety.
🌐
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…
Discussions

android - Kotlin: Iterate through a JSONArray - Stack Overflow
I'm writing an Android app using Kotlin and Realm. I have a JSONArray, and I want to iterate through the JSONObjects in this array in order to load them in a Realm database class: Realm class: impo... More on stackoverflow.com
🌐 stackoverflow.com
Best way to iterate a JSON in Kotlin - Stack Overflow
Then you can retrieve the different ... value = jsonObject.get("key"). The exact mechanism will depend on the JSON library you use, but the point is, use a JSON library. Someone else has done all the hard work of parsing JSON, you can just use the library and work with your JSON object. ... import com.fasterxml.jackson.module.kotlin.jacksonOb... More on stackoverflow.com
🌐 stackoverflow.com
java - Android JSONObject - How can I loop through a flat JSON object to get each key and value - Stack Overflow
Note: You can't use the short form ... nor JSONObject are iterable. :-( 2015-06-15T02:25:06.387Z+00:00 ... @PravinsinghWaghela pretty sure the OP asked how to loop through a json object. 2016-11-11T10:40:00.45Z+00:00 ... You'll need to use an Iterator to loop through the keys to get their values. Here's a Kotlin implementation, ... More on stackoverflow.com
🌐 stackoverflow.com
JSONObject & JSONArray extensions
Although these aren't in the android.* package, JSONObject / JSONArray are included as part of Android, and they are really quite a pain to work with in Kotlin (for example, JSONArray does not even implement Iterable, so none of Kotlin's... More on github.com
🌐 github.com
2
February 7, 2018
🌐
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 - Here, we add the iterator operator to JSONArray. Finally, this lets us loop over it like the usual way: for (book in booksJSONArray) { println("${(book as JSONObject).get("book_name")} by ${(book as JSONObject).get("author")}") }
🌐
TutorialsPoint
tutorialspoint.com › how-to-iterate-a-json-array-in-android-using-kotlin
How to iterate a JSON Array in Android using Kotlin?
import android.os.Bundle import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import org.json.JSONObject @Suppress("NAME_SHADOWING") class MainActivity : AppCompatActivity() { private lateinit var textView: TextView var strJson = ("{ \"Employee\" :[{\"ID\":\"01\",\"Name\":\"Sam\",\"Salary\":\"50000\"}," + "{\"ID\":\"02\",\"Name\":\"Shankar\",\"Salary\":\"60000\"}] }") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" textView = findViewById(R.id.textView) val data = String
🌐
Stack Overflow
stackoverflow.com › questions › 45810977 › best-way-to-iterate-a-json-in-kotlin › 45812199
Best way to iterate a JSON in Kotlin - Stack Overflow
Then you can retrieve the different values like String value = jsonObject.get("key"). The exact mechanism will depend on the JSON library you use, but the point is, use a JSON library. Someone else has done all the hard work of parsing JSON, you can just use the library and work with your JSON object. ... import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue ...
🌐
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 · 中文 – 简体
Find elsewhere
🌐
Appsloveworld
appsloveworld.com › kotlin › 100 › 148 › how-to-iterate-single-json-array-result-in-kotlin-android
How to iterate single JSON array result in Kotlin Android-kotlin
AppsLoveWorld Technologies provides software development and offer Free Resources for the developer like Programming Tutorial and Technology Reviews.
🌐
GitHub
github.com › android › android-ktx › issues › 241
JSONObject & JSONArray extensions · Issue #241 · android/android-ktx
February 7, 2018 - T)?.let { add(transform(it)) } } inline fun <reified T, R> JSONArray.mapIndexed(transform: (index: Int, T?) -> R): MutableList<R?> = mapIndexedTo(mutableListOf(), transform) inline fun <reified T, R> JSONArray.mapIndexedTo( to: MutableList<R?>, transform: (index: Int, T?) -> R? ): MutableList<R?> = (0..length()).mapTo(to, { i -> transform(i, get(i) as? T) } ) // Iteration inline fun <reified T> JSONArray.forEach(action: (T?) -> Unit) { (0..length()).forEach { action(get(it) as?
Author   ethanblake4
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
How to iterate list which has been unsafeCast-ed from JSON - Support - Kotlin Discussions
May 11, 2020 - Hello, I am struggling with simple for loop of list common data objects generated from JSON in Javascript. The error I get is: TypeError: “availableBooks.iterator is not a function” The list is generated this way: fun main() { MainScope().launch { val availableBooks = getAvailableBooks() val innerHtml = StringBuilder().append("Aplikacia je nacitana: ") console.log(availableBooks) println("${availableBooks::class}") for (availableBook in availa...
🌐
Edureka Community
edureka.co › home › community › categories › java › iterate over a jsonobject
Iterate over a JSONObject | Edureka Community
June 27, 2018 - JSON library called JSONObject is used(I don't mind switching if I need to) We know how to iterate over ... http://url3.com//", "shares": 15 } }
🌐
Kotlin
kotlinlang.org › api › kotlinx.serialization › kotlinx-serialization-json › kotlinx.serialization.json › -json-array
JsonArray | kotlinx.serialization – Kotlin Programming Language
Class representing JSON array, consisting of indexed values, where value is arbitrary JsonElement · Since this class also implements List interface, you can use traditional methods like List.get or List.getOrNull to obtain Json elements
🌐
Kotlin Discussions
discuss.kotlinlang.org › javascript
Iterating over Json properties - #3 by jvskriubakken
August 29, 2016 - Thanks for following up! This is what we have tried: private fun jsonToMap(json: Json): Map { val map: MutableMap = linkedMapOf() for (key in json) { map.put(key, json[key] as String) } // or for( (name, value) in json ) { map.put(name, value as String) } return map }
🌐
Android Developers
developer.android.com › api reference › jsonobject
JSONObject | API reference | Android Developers
February 26, 2026 - Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
GitHub
github.com › cbeust › klaxon
GitHub - cbeust/klaxon: A JSON parser for Kotlin · GitHub
val objectString = """{ "name" : "Joe", "age" : 23, "flag" : true, "array" : [1, 3], "obj1" : { "a" : 1, "b" : 2 } }""" JsonReader(StringReader(objectString)).use { reader -> reader.beginObject() { var name: String? = null var age: Int? = null var flag: Boolean? = null var array: List<Any> = arrayListOf<Any>() var obj1: JsonObject?
Starred by 1.9K users
Forked by 123 users
Languages   Kotlin
Top answer
1 of 2
4

You can cast it successfully, since JSONArray is-A Iterable. but it can't make sure each element in JSONArray is a JSONObject.

The JSONArray is a raw type List, which means it can adding anything, for example:

val jsonArray = JSONArray()
jsonArray.add("string")
jsonArray.add(JSONArray())

When the code operates on a downcasted generic type Iterable<JSONObject> from a raw type JSONArray, it maybe be thrown a ClassCastException, for example:

val jsonObjectIterable = jsonArray as Iterable<JSONObject>

//    v--- throw ClassCastException, when try to cast a `String` to a `JSONObject`
val first = jsonObjectIterable.iterator().next()

So this is why become dangerous. On the other hand, If you only want to add JSONObjecs into the JSONArray, you can cast a raw type JSONArray to a generic type MutableList<JSONObject>, for example:

@Suppress("UNCHECKED_CAST")
val jsonArray = JSONArray() as MutableList<JSONObject>

//      v--- the jsonArray only can add a JSONObject now
jsonArray.add(JSONObject(mapOf("foo" to "bar")))

//      v--- there is no need down-casting here, since it is a Iterable<JSONObject>
val jsonObjectIterable:Iterable<JSONObject> = jsonArray 

val first = jsonObjectIterable.iterator().next()

println(first["foo"])
//           ^--- return "bar"
2 of 2
1

Following is rather a simple implementation.

Suppose Your Object is Person

data class Person( val ID: Int, val name: String): Serializable
val gson = Gson()
val persons: Array<Person> = gson.fromJson(responseSTRING, Array<Person>::class.java)

Now persons is an Array of Person

🌐
Coderanch
coderanch.com › t › 736190 › languages › Loop-JsonNode-fields-Kotlin
Loop through a JsonNode and its fields [Kotlin] (Kotlin forum at Coderanch)
November 2, 2020 - Each iteration should present a JsonNode with its elements until no JsonNode object is found.