How to check if a key exists in Json Object

Posted in :

to find key is exist or not in JsonObjecthas("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.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *