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

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

Check if Json Array is empty or contains one Json Object

问题

这是我的代码片段:

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

How can I check whether

  1. {
  2. "data": [
  3. {
  4. "latitude": 12,
  5. "longitude": 13
  6. }
  7. ]
  8. }

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

  1. {
  2. "data": [
  3. []
  4. ]
  5. }

This is my snippet:

  1. ...
  2. JSONObject jsonObject = new JSONObject(response.body());
  3. JSONArray jsonArray = jsonObject.getJSONArray("data");
  4. ...

答案1

得分: 1

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

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

You can check the data array length like this:

  1. 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:

确定