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 . If you are using optString("key") method to get String value then don't worry about keys are existing or not in the JsonObject . Note that you can check only root keys with has(). Get values with get().
We can have duplicate keys in a JSON object, and it would still be valid. The validity of duplicate keys in JSON is an exception and not a rule, so this becomes a problem when it comes to actual implementations.
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
.
Use:
if (containerObject.has("video")) { //get value of video }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With