I have a string which holds a json response. How C...
# android
m
I have a string which holds a json response. How Can I convert or cast it into JsonObject in Kotlin? var string : String = "{\n" + " \"success\": true,\n" + " \"response\": {\n" + " \"message\": \"Login is valid.\"\n" + " }\n" + "}"
d
#klaxon library or #kotson?
e
Just the same way as you do in Java
d
btw, if you use triple quotes, you dont have to put that escaping everywhere
Copy code
val breakerEnabled = """
        {
          "CmdEnabled": "true",
          "CurrState": "Closed"
        }
    """
m
Thanks guys, I got it. Same as java var jsonObj = JSONObject("{\n" + " \"success\": true,\n" + " \"response\": {\n" + " \"message\": \"Login is valid.\"\n" + " }\n" + "}")
d
I use #klaxon to get json into data classes for api contracts, I had tried the Java version of Gson, but I ended up with nulls in non-null vars... its easier to work with data classes than JsonObject when the structure is known, at also acts like an extra check that you're really getting what you're expecting... Klaxon even works well with sealed classes, so various response formats can be modeled and validated very simply...
l
try #jackson-kotlin, it is mature and have kotlin support module