Home > Software design >  Kotlin - Get value in array in JSON Object android
Kotlin - Get value in array in JSON Object android

Time:01-19

i'm new to Kotlin and currently still learning about consuming REST API.

So, I have a JSON like this

{
"timestamp": 1642567386351,
"code": "OK",
"status": 200,
"data": [
    {
        "id": 1,
        "name": "Our Business",
        "validated": false,
        "active": true
    },
    {
        "id": 5,
        "name": "belum ada",
        "validated": false,
        "active": true
    },
    {
        "id": 31,
        "name": "arcase",
        "validated": false,
        "active": true
    },
    {
        "id": 34,
        "name": "arcase",
        "validated": true,
        "active": true
    },
    {
        "id": 40,
        "name": "arstore",
        "validated": false,
        "active": true
    },
    {
        "id": 43,
        "name": "arstore",
        "validated": false,
        "active": true
    },
    {
        "id": 54,
        "name": "Chocolate Factory",
        "validated": true,
        "active": true
    },
    {
        "id": 70,
        "name": "Appintimedia",
        "validated": false,
        "active": true
    },
    {
        "id": 74,
        "name": "Toystory",
        "validated": false,
        "active": true
    },
    {
        "id": 77,
        "name": "RohanahStore",
        "validated": false,
        "active": true
    },
    {
        "id": 80,
        "name": "Testing Rohanah",
        "validated": false,
        "active": true
    },
    {
        "id": 83,
        "name": "Test-debug",
        "validated": true,
        "active": true
    },
    {
        "id": 86,
        "name": "Rohanah Testing",
        "validated": true,
        "active": true
    },
    {
        "id": 89,
        "name": "Yusdi",
        "validated": false,
        "active": true
    },
    {
        "id": 92,
        "name": "Honeybe",
        "validated": true,
        "active": true
    },
    {
        "id": 95,
        "name": "Test sajh",
        "validated": false,
        "active": true
    },
    {
        "id": 98,
        "name": "Ideku Business Testing A",
        "validated": false,
        "active": true
    },
    {
        "id": 101,
        "name": "Honeybe Tangerang",
        "validated": true,
        "active": true
    },
    {
        "id": 107,
        "name": "Appinzpire",
        "validated": false,
        "active": true
    },
    {
        "id": 110,
        "name": "Toystory",
        "validated": false,
        "active": true
    }
]

}

I want to get the value of name in this JSON. Still don't know how to getting this value. I used volley for getting the HTTP Responses. I used axios in javascript before and I thought that I can use the same way to accessing the name value.

can any body help me with this?

thanks in advance

CodePudding user response:

I will explain how to access every object inside the JSON using Kotlin, having known that there isn't any problem receiving it

first create a data class

data class JSON_data(
    @Json(name = "timestamp") val timestamp: String,
    @Json(name = "code") val code: String,
    @Json(name = "status") val status: Int,
    @Json(name = "data") val data: List<my_data>
)

So we will get a List named data from the API. We should now create a data class for objects in the list

data class my_data(
    @Json(name = "id") val id: Int,
    @Json(name = "name") val name: String,
    @Json(name = "validate") val validate: Boolean,
    @Json(name = "active") val active: Boolean
)

So in your MainActivity, you can get an instance of data and you can access the objects Eg: You can get the name of the first item in data list like this data[0].name

CodePudding user response:

In The Success block you can get the Json Array and loop the json array to get the name

val arr = json.getJSONArray("data")
            var i = 0
            while (i < arr.length()) {
                val item = arr.getJSONObject(i)
                item.get("name")
                i  
            }
  •  Tags:  
  • Related