Prashanta Das
Prashanta Das

Reputation: 11

Get particular key value from json in android (kotlin)

I'm beginner in android (Kotlin).I am getting the Following json from the HTTP Response.

{
    "status": 1,
    "userDetails": [
        {
            "AL_ID": "2",
            "User_Id": "admin",
            "User_Password": "admin",
            "Created_Date": "2020-07-30 11:23:55"
        }
    ],
    "lastlogin": "2020-08-20 12:29:47"
}

I want to get status value from this json. How to get it.

Upvotes: 1

Views: 8245

Answers (2)

I.Step
I.Step

Reputation: 779

I would suggest using Gson library too, for android development in kotlin.

Also I would suggest to have the same DATA classes in kotlin as in database. In that way you can map objects fromJson and toJson with one line of code.

I found article that explains in details how to use Gson library with kotlin.

https://medium.com/@hissain.khan/parsing-with-google-gson-library-in-android-kotlin-7920e26f5520

Upvotes: 0

Siddharth Kamaria
Siddharth Kamaria

Reputation: 2717

You can use the built-in JSONObject and do something as shown below.

val json = JSONObject(jsonString) // String instance holding the above json
val status = json.getInt("status")

Having said that, I'd recommend you to take a look at Gson (for JSON serialization and deserialization) and Retrofit (an HTTP client for Android).

Upvotes: 5

Related Questions