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!
Use the exceptions to your advantage
http://www.json.org/javadoc/org/json/JSONObject.html#getJSONObject(java.lang.String)
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.
You could also go step by step, if you want to print logs for each level;