I'm checking if an object in a JSON string exists using this:
JSONObject json = null;
try {
json = new JSONObject(myJsonString);
} catch (JSONException e) { e.printStackTrace(); }
if(json.has("myObject")) System.out.println("EXISTS");
else System.out.println("DOESN'T EXIST");
The problem appears when I attempt to check if a sub object exists. e.g:
...,"queue":{"building":{"q0":{"id":177779,...
Queue always exists and building also, but q0 is not always there. So, how can I check the existence of q0? And, is there a way to check it using the Gson library?
Thank you in advance!
jquery json provide several method for getting key value or check if key exists etc. In this example we will use hasOwnProperty method of json object that will help to check if key exists or not in jquery. hasOwnProperty return true if key is exists and return false if key is not exists on given javascript json.
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().
JSON. stringify method is used to convert a JavaScript object to a JSON string. So we can use it to convert an object to a string, and we can compare the result with {} to check if the given object is empty.
Sometimes, you need to check a parsed JSON object or a variable to see if it's an array before iteration or before any other manipulation. You can use the Array. isArray method to check if a variable is an array.
You can simply give it a try and return null if the try failed. Or you can break your attempt up into little pieces to monitor where it fails.
/**
* This method will return the JSONObject q0, if it exists
* If it doesn't exist it will return NULL
*
*/
private JSONObject getQZero(JSONObject json)
{
try
{
return json.getJSONObject("queue").getJSONObject("building").getJSONObject("q0");
}
catch (JSONException e)
{
// This could be triggered either because there is no q0
// or because the JSON structure is different from what was expected.
return null;
}
}
You could also go step by step, if you want to print logs for each level;
/**
* This method will show where your jsonparsing fails.
* It will throw a JSONOException if the json is way different from what
* was expected, and otherwise it will print a log of where the parsing
* failed.
*/
private JSONObject getQZero(JSONObject json) throws JSONException
{
// Stop if no queue
if (! myObject.has("queue")
{
Log.d(TAG, "no queue!");
return null;
}
JSONObject queue = myObject.getJSONObject("queue");
// Stop if no building
if (! queue.has("building")
{
Log.d(TAG, "no building!");
return null;
}
JSONObject building = queue.getJSONObject("building")
// Stop if no q0
if (! building.has("q0"))
{
Log.d(TAG, "no q0!");
return null;
}
JSONObject q0 = building.getJSONObject("q0");
// Q0 is returned here. If the method returned earlier, it returned NULL
// You could also do nested ifs, but the indentation gets crazy
return q0;
}
Use the exceptions to your advantage
try {
JSONObject i = json. getJSONObject("q0");
// Is there do something
} catch (JSONException e) {
// Isn't there
}
http://www.json.org/javadoc/org/json/JSONObject.html#getJSONObject(java.lang.String)
JSONException - if the key is not found or if the value is not a JSONObject.
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