且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查键是否存在于Json Object中并获取其值

更新时间:2023-11-25 23:53:10

使用以下代码查找键是否在JsonObject中存在. has("key")方法用于在JsonObject中查找键.

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");
}

如果使用optString("key")方法获取String值,则不必担心JsonObject中是否存在键.

If you are using optString("key") method to get String value then don't worry about keys are existing or not in the JsonObject.