Ahmet Özrahat
Ahmet Özrahat

Reputation: 335

Kotlin - Parse JSON

I have a json string with 2 keys error and user. First I want to check if error is not false and get the values from user.

Here is the Json String:

{
    "error": false,
    "user": {
        "id": 26,
        "name": "Someone",
        "email": "[email protected]",
        "aktif": 1
    }
}

How can I achieve this ?

Upvotes: 0

Views: 4272

Answers (1)

ʍѳђઽ૯ท
ʍѳђઽ૯ท

Reputation: 16976

Get the JsonObject "error" first :

val errorCheck = yourjsonresult.getJSONObject("error"); 

Then compare to check if it was false then:

if(errorCheck.equals("false")) { // or if it wasn't false -> !errorCheck.equals("false"))

    val data = yourjsonresult.getJsonObject("user"); // get the user object
    val name = data?.getString("name"); // or the other items
}

The result should be:

Someone

Also, arrays starts by [ but in your case, those are json objects which starts-ends by {}.

Upvotes: 2

Related Questions