0

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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++
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.