I have some JSON like so:
{"result":[{"job":{"type":"Employee","title":"","occupation":"Underwater Basket Weaver"}}]}
I am getting the occupation value like so:
import org.json.JSONArray;
import org.json.JSONObject;
String occupation = null;
JSONArray resultArray = obj.getJSONArray("result");
JSONObject firstResult = resultArray.getJSONObject(0);
occupation = firstResult.getJSONObject("job").getString("occupation");
However, for some reason, the occupation value is not always a String. My guess is that is could be an int or it could be null. I end up with an exception like this:
org.json.JSONException: JSONObject["occupation"] not a string. at org.json.JSONObject.getString(JSONObject.java:658)
What should you do when you are dealing with JSONObjects that take on variable data types?

getString("occupation")is valid withisNull(String). According to json.org/javadoc/org/json/… ,getString(...)throws the exception if it doesn't find one