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.
android - Kotlin: Iterate through a JSONArray - Stack Overflow
Best way to iterate a JSON in Kotlin - Stack Overflow
java - Android JSONObject - How can I loop through a flat JSON object to get each key and value - Stack Overflow
JSONObject & JSONArray extensions
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
}
Even if some class doesn't expose an iterator method, you can still iterate it with for statement by providing an extension function iterator:
operator fun JSONArray.iterator(): Iterator<JSONObject>
= (0 until length()).asSequence().map { get(it) as JSONObject }.iterator()
Now when you use JSONArray in for statement this extension is invoked to get an iterator. It creates a range of indices and maps each index to an item corresponding to this index.
I suppose the cast to JSONObject is required as the array can contain not only objects but also primitives and other arrays. And the asSequence call is here to execute map operation in a lazy way.
Generic way (assuming all array entries are of same type)
@Suppress("UNCHECKED_CAST")
operator fun <T> JSONArray.iterator(): Iterator<T>
= (0 until length()).asSequence().map { get(it) as T }.iterator()
Use the keys() iterator to iterate over all the properties, and call get() for each.
Iterator<String> iter = json.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
Object value = json.get(key);
} catch (JSONException e) {
// Something went wrong!
}
}
Short version of Franci's answer:
for(Iterator<String> iter = json.keys();iter.hasNext();) {
String key = iter.next();
...
}
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"
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