2

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.

2 Answers 2

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

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

Comments

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

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.