showing results for - "if condition to whether json object has jsonarray or jsonobject"
Beatrice
09 Oct 2018
1JSONObject json = new JSONObject(jsonString);
2
3if (json.has("data")) {
4
5    JSONObject dataObject = json.optJSONObject("data");
6
7    if (dataObject != null) {
8
9        //Do things with object.
10
11    } else {
12
13        JSONArray array = json.optJSONArray("data");
14
15        //Do things with array
16    }
17} else {
18    // Do nothing or throw exception if "data" is a mandatory field
19}