【问题标题】:How to get size of a response coming from json array如何获取来自json数组的响应大小
【发布时间】:2019-07-29 04:20:29
【问题描述】:

我有一个 json 响应,其中存储了一些数据,现在我想获取 json 数组中存在的 json 对象的大小

[
    {
        "type": "Insurance Policies",
        "count": 2,
        "category_id": "1"
    },
    {
        "type": "Mediclaim Policies",
        "count": 1,
        "category_id": "2"
    },
    {
        "type": "Mutual Fund Policies",
        "count": 1,
        "category_id": "3"
    }
]

【问题讨论】:

  • 如果您使用自动解析器,则列表大小为 JsonArray 大小。如果您手动解析,则 jsonArray.length().
  • 好的,我得到了答案,我只需要创建 JSONArray 的对象并将响应存储在该对象中,只需要调用 object.length()

标签: android json retrofit2


【解决方案1】:

使用以下代码获取 JSON 数组长度

String jsonStringContent = [
    {
        "type": "Insurance Policies",
        "count": 2,
        "category_id": "1"
    },
    {
        "type": "Mediclaim Policies",
        "count": 1,
        "category_id": "2"
    },
    {
        "type": "Mutual Fund Policies",
        "count": 1,
        "category_id": "3"
    }
];

JSONArray jsonArray = new JSONArray(jsonStringContent);

int size = jsonArray.length();

【讨论】:

    【解决方案2】:

    好的,我得到了答案,我只需要创建 JSONArray 的对象并将响应存储在该对象中,只需要调用 object.length()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-03
      • 2020-01-23
      • 1970-01-01
      • 2021-01-08
      相关资源
      最近更新 更多