Check If Jsonobject Key Exists
I'm making api calls to a back-end who always has a JSONArray series and sometimes has a JSONArray places. In the code below, I am trying to write an if statement that says, whenev
Solution 1:
Check if the object has a key called places.
if (passingObject.has("places") {
JSONArray places = passingObject.getJSONArray("places");
} else {
askServerAgain();
}
Solution 2:
Or you can use http://www.json.org/javadoc/org/json/JSONObject.html#optJSONArray(java.lang.String)
JSONArray places = passingObject.optJSONArray("places");
if(places == null){...}
optJSONArray();
It returns null if there is no such key, or if its value is not a JSONArray.
Post a Comment for "Check If Jsonobject Key Exists"