How to check the type of a value from a JSONObject?

You can get the object from the JSON with the help of JSONObject.get() method and then using the instanceof operator to check for the type of Object.

Something on these lines:-

String jString = "{\"a\": 1, \"b\": \"str\"}";
JSONObject jObj = new JSONObject(jString);
Object aObj = jObj.get("a");
if (aObj instanceof Integer) {
    // do what you want
}

Leave a Comment