Digamos que este es mi JSON Object
{ "LabelData": { "slogan": "AWAKEN YOUR SENSES", "jobsearch": "JOB SEARCH", "contact": "CONTACT", "video": "ENCHANTING BEACHSCAPES", "createprofile": "CREATE PROFILE" } }Necesito saber si el 'video' existe o no en este objeto, y si existe, necesito obtener el valor de esta clave. Intenté seguir, pero no puedo obtener el valor de esta clave.
containerObject= new JSONObject(container); if(containerObject.hasKey("video")){ //get Value of video }Use el código a continuación para encontrar la clave si existe o no en JsonObject . El método has("key") se usa para encontrar claves en JsonObject .
containerObject = new JSONObject(container); //has method if (containerObject.has("video")) { //get Value of video String video = containerObject.optString("video"); } Si está utilizando optString("key") para obtener el valor de String, no se preocupe si las claves existen o no en el JsonObject .
Usar:
if (containerObject.has("video")) { //get value of video }containerObject = new JSONObject(container); if (containerObject.has("video")) { //get Value of video }