반응형

https://stackoverflow.com/questions/36184641/kotlin-iterate-through-a-jsonarray/36188796


Realm class:

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"
        }
    ]
}

아래 코드를 이용하여 JSON array 내부 오브젝트 사용
for (i in 0..(persons.length() - 1)) {
    val item = persons.getJSONObject(i)

    // Your code here
} 


반응형

+ Recent posts