1

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.

deHaar
  • 17,687
  • 10
  • 38
  • 51
Prashanta Das
  • 11
  • 1
  • 2

2 Answers2

5

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).

Siddharth Kamaria
  • 2,448
  • 2
  • 17
  • 37
0

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

I.Step
  • 613
  • 6
  • 22