Java 如何检查 Json 对象中是否存在键并获取其值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43341806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:51:17  来源:igfitidea点击:

How to check if a key exists in Json Object and get its value

javaandroidjsonperformanceandroid-layout

提问by dev90

Let say this is my JSON Object

让说这是我的 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.

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

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

采纳答案by Chetan Joshi

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")方法来获取字符串值,那么不要担心 .key 文件中是否存在键JsonObject

回答by Er.Tyson Subba

Use:

用:

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

回答by Mukeshkumar S

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

回答by Tariq

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
    }
}

回答by Chetan Pushpad

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

回答by Rizwan

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

我想这可能对你有帮助

回答by Pehlaj

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) {

    }

回答by Fathima km

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”的方法。如果此对象具有名称映射,则返回 true。映射可能为 NULL。 http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)