32

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
}
dev90
  • 7,187
  • 15
  • 80
  • 153

8 Answers8

60

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.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • Note that you can check only root keys with has(). Get values with get(). – Zon May 23 '17 at 11:57
  • if you dont find the containerObject.has you can try containerObject.containsKey.. I did this.. – Vijay Jul 11 '20 at 20:32
11

Use:

if (containerObject.has("video")) {
    //get value of video
}
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
TysonSubba
  • 1,362
  • 2
  • 12
  • 16
5
containerObject = new JSONObject(container);
if (containerObject.has("video")) { 
   //get Value of video
}
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Mukeshkumar S
  • 785
  • 1
  • 14
  • 30
5

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
    }
}
Tariq
  • 2,489
  • 11
  • 36
  • 61
5

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();
 }
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
Chetan Pushpad
  • 345
  • 1
  • 9
4

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

    }
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
4

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)

Fathima km
  • 2,539
  • 3
  • 18
  • 26
1
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

Rizwan
  • 163
  • 2
  • 8