检查 JSON 数组是否为空或包含一个 JSON 对象

huangapple go评论61阅读模式
英文:

Check if Json Array is empty or contains one Json Object

问题

这是我的代码片段:

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

How can I check whether

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

contains one object, as the previous example, or it is empty, as the following example:

{
         "data": [
              []
         ]
}

This is my snippet:

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

答案1

得分: 1

你可以这样检查数据数组的长度:

if (jsonArray.length() > 0) { //这意味着数据属性中有记录 }
英文:

You can check the data array length like this:

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

huangapple
  • 本文由 发表于 2020年10月3日 04:03:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64177622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定