简体   繁体   中英

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": "someone@gmail.com",
        "aktif": 1
    }
}

How can I achieve this ?

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 {} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM