Я пишу приложение для Android, используя Kotlin и Realm. У меня есть JSONArray, и я хочу, чтобы перебирать JSONObjects в этом массиве, чтобы загрузить их в классе базы данных Realm:Kotlin: Итерация через JSONArray
Realm класса:
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import io.realm.annotations.Required
open class Person(
@PrimaryKey open var id: Long = 0,
@Required
open var name: String = ""
) : RealmObject() {
}
The JSONArray:
{
"persons":[
{
"id":0,
"name":"Biatrix"
},
{
"id":1,
"name":"Bill"
},
{
"id":2,
"name":"Oren"
},
{
"id":3,
"name":"Budd"
}
]
}
Я попытался итерация, как следующее:
for (item : JSONObject in persons) {
}
... но я получаю ошибку for-loop range must have an iterator() method
.
Эта библиотека JSON вы используете? – mfulton26
Я использую 'org.json.JSONArray' и' org.json.JSONObject'. – Ambran