📜  if条件判断json对象是否有jsonarray或jsonobject - Javascript代码示例

📅  最后修改于: 2022-03-11 15:04:00.708000             🧑  作者: Mango

代码示例1
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
}