Let say this is my JSON Object

{
  "LabelData": {
    "slogan": "AWAKEN YOUR SENSES",
    "jobsearch": "JOB SEARCH",
    "contact": "CONTACT",
    "video": "ENCHANTING BEACHSCAPES",
    "createprofile": "CREATE PROFILE"
  }
}

I need to know that either 'video` exists or not in this Object, and if it exists i need to get the value of this key. I have tried following, but i am unable to get value of this key.

 containerObject= new JSONObject(container);
 if(containerObject.hasKey("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.

Json相关问答推荐

您可以使用Jolt对JSON执行OR条件吗

JSON API返回多个数组,需要帮助拼合数据以存储在SQL Server数据库表中

无法从JSON解析ZonedDateTime,但可以使用格式化程序很好地解析

织女星-没有循环的动画条形图第二部分(实际上是织女星)

在Ruby的json中压缩单个字段

当列为空时从 SQL 获取 JSON

Moshi:序列化 List 时出现问题

爆炸没有数组的 struct pyspark

使用动态语言jQuery:根据匹配模式提取与其他值匹配的值

将带有::text[]的JSON数组转换未按预期工作

使用 jq 和脚本 bash 映射两个 json

使用 json_query 过滤嵌套列表中的元素

如何配置spring mvc 3在json响应中不返回null对象?

Retrofit 2.0 如何解析嵌套的 JSON 对象?

Jackson Json:如何将数组转换为 JsonNode 和 ObjectNode?

在 Postgres 中向 JSON 对象添加元素

如何创建 JSON 对象 Node.js

如何向(JSON)对象的原型添加方法?

我可以使用空字符串作为对象标识符吗?

为什么 jqXHR.responseText 返回字符串而不是 JSON 对象?