繁体   English   中英

检查 Json Array 是否为空或包含一个 Json 对象

[英]Check if Json Array is empty or contains one Json Object

我如何检查是否

{
     "data": [
          {
               "latitude": 12,
               "longitude": 13
          }
     ]
}

包含一个对象,如前一个示例,或者它是空的,如以下示例:

{
         "data": [
              []
         ]
}

这是我的片段:

...
JSONObject jsonObject = new JSONObject(response.body());
JSONArray jsonArray = jsonObject.getJSONArray("data");
...

您可以像这样检查数据数组长度:

if(jsonArray.length() > 0) { //it means that there is a record in data attr }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM