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 OverflowUnfortunately, 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();
...
}
try {
JSONObject json = new JSONObject(data);
Iterator<String> temp = json.keys();
while (temp.hasNext()) {
String key = temp.next();
Object value = json.get(key);
}
} catch (JSONException e) {
e.printStackTrace();
}
Thanks @oldfell;
JSONObject object = new JSONObject(json);
Iterator keys = object.keys();
while(keys.hasNext()) {
String dynamicKey = (String)keys.next();
JSONObject line = object.getJSONObject(dynamicKey);
String desc = line.getString("desc");
}
I solved the problem in this way
There is no question that the future of parsing in Kotlin will be with kotlinx.serialization. It is part of Kotlin libraries. Version kotlinx.serialization 1.0 is finally released
https://github.com/Kotlin/kotlinx.serialization
Copyimport kotlinx.serialization.*
import kotlinx.serialization.json.Json
@Serializable
data class MyModel(val a: Int, @Optional val b: String = "42")
fun main(args: Array<String>) {
// serializing objects
val jsonData = Json.encodeToString(MyModel.serializer(), MyModel(42))
println(jsonData) // {"a": 42, "b": "42"}
// serializing lists
val jsonList = Json.encodeToString(MyModel.serializer().list, listOf(MyModel(42)))
println(jsonList) // [{"a": 42, "b": "42"}]
// parsing data back
val obj = Json.decodeFromString(MyModel.serializer(), """{"a":42}""")
println(obj) // MyModel(a=42, b="42")
}
You can use this library https://github.com/cbeust/klaxon
Klaxon is a lightweight library to parse JSON in Kotlin.