1

我有以下 JSON 字符串。

{
  "data": [
    {
      "city_id": "1",
      "city_name_eng": "Multan",
      "0": {
        "category_id": "1",
        "city_id": "1",
        "category_name_eng": "Mango",
        "0": {
          "product_id": "1",
          "category_id": "1",
          "product_name_eng": "Mango1"
        },
        "1": {
          "product_id": "2",
          "category_id": "1",
          "product_name_eng": "Mango2"
        },
        "2": {
          "product_id": "3",
          "category_id": "1",
          "product_name_eng": "Mango3"
        },
        "3": {
          "product_id": "4",
          "category_id": "1",
          "product_name_eng": "Mango1"
        }
      },
      "1": {
        "category_id": "2",
        "city_id": "1",
        "category_name_eng": "Shoes"
      },
      "2": {
        "category_id": "3",
        "city_id": "1",
        "category_name_eng": "Bank"
      }
    },
    {
      "city_id": "2",
      "city_name_eng": "Lahore",
      "3": {
        "category_id": "4",
        "city_id": "2",
        "category_name_eng": "Food"
      },
      "4": {
        "category_id": "5",
        "city_id": "2",
        "category_name_eng": "Computer"
      },
      "5": {
        "category_id": "6",
        "city_id": "2",
        "category_name_eng": "Mobile"
      }
    }
  ]
}

我想要 JSONObject 的长度。基本上,第一个对象"city_id": "1" 有 4 个嵌套的 JSONObjects 和 2 个值,即 city_id 和 city_name_eng。当我使用objJSON.length()时,它给了我 6 的长度。但相反,我想要 4 个嵌套的 JSONObjects,我怎样才能在运行时得到它?(嵌套的 jsonobjects 是 3 还是 4 或任何数字)

如何区分嵌套的 JSONObjects 和键值?

4

1 回答 1

1

如果该网站是您自己的,那么我建议修改代码以在适当的地方返回JSONArrays。

否则,jsonObj.keys()将返回一个Iterator用于迭代键并解析对象的方法。

于 2012-10-08T08:20:42.793 回答