如何检查Json对象中是否存在密钥并获取其值[重复]

时间:2022-03-24 11:13:03

This question already has an answer here:

这个问题在这里已有答案:

Let say this is my JSON Object

假设这是我的JSON对象

{
  "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.

我需要知道这个对象中是否存在'video`,如果存在,我需要获取此密钥的值。我试过跟随,但我无法获得这个键的价值。

 containerObject= new JSONObject(container);
 if(containerObject.hasKey("video")){
  //get Value of video
}

8 个解决方案

#1


9  

Use below code to find key is exist or not in JsonObject. has("key") method is used to find keys in JsonObject.

使用下面的代码来查找JsonObject中是否存在密钥。 has(“key”)方法用于在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.

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

#2


4  

Use

使用

if(containerObject.has("video")){
    //get value of video
}

#3


3  

 containerObject= new JSONObject(container);
 if(containerObject.has("video")){ 
     //get Value of video
 }

#4


2  

From the structure of your source Object, I would try:

从源对象的结构,我会尝试:

containerObject= new JSONObject(container);
 if(containerObject.has("LabelData")){
  JSONObject innerObject = containerObject.getJSONObject("LabelData");
     if(innerObject.has("video")){
        //Do with video
    }
}

#5


2  

Try

尝试

private boolean hasKey(JSONObject jsonObject, String key) {
    return jsonObject != null && jsonObject.has(key);
}

  try {
        JSONObject jsonObject = new JSONObject(yourJson);
        if (hasKey(jsonObject, "labelData")) {
            JSONObject labelDataJson = jsonObject.getJSONObject("LabelData");
            if (hasKey(labelDataJson, "video")) {
                String video = labelDataJson.getString("video");
            }
        }
    } catch (JSONException e) {

    }

#6


2  

JSONObject class has a method named "has". Returns true if this object has a mapping for name. The mapping may be NULL. http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)

JSONObject类有一个名为“has”的方法。如果此对象具有name的映射,则返回true。映射可以是NULL。 http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)

#7


2  

Please try this one..

请试试这个..

JSONObject jsonObject= null;
try {
     jsonObject = new JSONObject("result........");
     String labelDataString=jsonObject.getString("LabelData");
     JSONObject labelDataJson= null;
     labelDataJson= new JSONObject(labelDataString);
     if(labelDataJson.has("video")&&labelDataJson.getString("video")!=null){
       String video=labelDataJson.getString("video");
     }
    } catch (JSONException e) {
      e.printStackTrace();
 }

#8


0  

JSONObject root= new JSONObject();
JSONObject container= root.getJSONObject("LabelData");

try{
//if key will not be available put it in the try catch block your program 
 will work without error 
String Video=container.getString("video");
}
catch(JsonException e){

 if key will not be there then this block will execute

 } 
 if(video!=null || !video.isEmpty){
  //get Value of video
}else{
  //other vise leave it
 }

i think this might help you

我想这可能会对你有所帮助

#1


9  

Use below code to find key is exist or not in JsonObject. has("key") method is used to find keys in JsonObject.

使用下面的代码来查找JsonObject中是否存在密钥。 has(“key”)方法用于在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.

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

#2


4  

Use

使用

if(containerObject.has("video")){
    //get value of video
}

#3


3  

 containerObject= new JSONObject(container);
 if(containerObject.has("video")){ 
     //get Value of video
 }

#4


2  

From the structure of your source Object, I would try:

从源对象的结构,我会尝试:

containerObject= new JSONObject(container);
 if(containerObject.has("LabelData")){
  JSONObject innerObject = containerObject.getJSONObject("LabelData");
     if(innerObject.has("video")){
        //Do with video
    }
}

#5


2  

Try

尝试

private boolean hasKey(JSONObject jsonObject, String key) {
    return jsonObject != null && jsonObject.has(key);
}

  try {
        JSONObject jsonObject = new JSONObject(yourJson);
        if (hasKey(jsonObject, "labelData")) {
            JSONObject labelDataJson = jsonObject.getJSONObject("LabelData");
            if (hasKey(labelDataJson, "video")) {
                String video = labelDataJson.getString("video");
            }
        }
    } catch (JSONException e) {

    }

#6


2  

JSONObject class has a method named "has". Returns true if this object has a mapping for name. The mapping may be NULL. http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)

JSONObject类有一个名为“has”的方法。如果此对象具有name的映射,则返回true。映射可以是NULL。 http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)

#7


2  

Please try this one..

请试试这个..

JSONObject jsonObject= null;
try {
     jsonObject = new JSONObject("result........");
     String labelDataString=jsonObject.getString("LabelData");
     JSONObject labelDataJson= null;
     labelDataJson= new JSONObject(labelDataString);
     if(labelDataJson.has("video")&&labelDataJson.getString("video")!=null){
       String video=labelDataJson.getString("video");
     }
    } catch (JSONException e) {
      e.printStackTrace();
 }

#8


0  

JSONObject root= new JSONObject();
JSONObject container= root.getJSONObject("LabelData");

try{
//if key will not be available put it in the try catch block your program 
 will work without error 
String Video=container.getString("video");
}
catch(JsonException e){

 if key will not be there then this block will execute

 } 
 if(video!=null || !video.isEmpty){
  //get Value of video
}else{
  //other vise leave it
 }

i think this might help you

我想这可能会对你有所帮助