IT기술 관련/모바일
kotlin 으로 json array response 처리하는 방법
호레
2019. 6. 26. 23:16
반응형
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
}
반응형