Answers for "if condition to whether json object has jsonarray or jsonobject"

-1

if condition to whether json object has jsonarray or jsonobject

JSONObject json = new JSONObject(jsonString);

if (json.has("data")) {

    JSONObject dataObject = json.optJSONObject("data");

    if (dataObject != null) {

        //Do things with object.

    } else {

        JSONArray array = json.optJSONArray("data");

        //Do things with array
    }
} else {
    // Do nothing or throw exception if "data" is a mandatory field
}
Posted by: Guest on July-26-2020

Code answers related to "if condition to whether json object has jsonarray or jsonobject"

Code answers related to "Javascript"

Browse Popular Code Answers by Language