How to check if a key exists in Json Object and get its value
Use:
if (containerObject.has("video")) {
//get value of video
}
Use below code to find key is exist or not in JsonObject
. has("key")
method is used to find keys in JsonObject
.
containerObject = new JSONObject(container);
//has method
if (containerObject.has("video")) {
//get Value of video
String video = containerObject.optString("video");
}
If you are using optString("key")
method to get String value then don't worry about keys are existing or not in the JsonObject
.