I have a text json that I recover from an API, but I can not decode it, because in the json I have only one object and not several:
{
"address": "[email protected]",
"username": "mickaelnanah",
"domain": "gmail.com",
"md5Hash": "0f6082627bfdeb56a3792f52ce8f0cb8",
"validFormat": true,
"deliverable": true,
"fullInbox": false,
"hostExists": true,
"catchAll": false,
"disposable": false,
"free": true
}
my code:
val jsonObj = JSONObject(jSonString.substring(jSonString.indexOf("{"), jSonString.lastIndexOf("}") + 1))
val mail = Email(jsonObj.getJSONObject("deliverable") as String)
println(mail.email)
Error:
Exception in thread "main" org.json.JSONException: JSONObject["deliverable"] is not a JSONObject.
I understand the error, it's because I do not have a name to the object, how can I directly take the fields?
SOLVED:
val jsonObj = JSONObject(jSonString.substring(jSonString.indexOf("{"), jSonString.lastIndexOf("}") + 1))
val mail: String = jsonObj.get("deliverable").toString()