在数组中搜索NSDictionary的复杂集合

时间:2011-11-11 08:56:56

标签: nsdictionary

我有一系列词典,类似于以下内容:

{
  "categories": [
    {
      "category_id": "1",
      "parent_id": "0",
      "en_name": "Category 1",
      "sub": [
        {
          "category_id": "2",
          "parent_id": "1",
          "en_name": "Abc Pqr",
          "businessCount": "5"
        },
        {
          "category_id": "5",
          "parent_id": "1",
          "en_name": "Xyz Model",
          "businessCount": "7"
        }
      ]
    },
    {
      "category_id": "3",
      "parent_id": "0",
      "en_name": "Category 2",
      "sub": [
        {
          "category_id": "4",
          "parent_id": "1",
          "en_name": "Test",
          "businessCount": "15"
        },
        {
          "category_id": "6",
          "parent_id": "1",
          "en_name": "Dummy",
          "businessCount": "9"
        }
      ]
    }
  ]
}

我想搜索元素“sub”的“en_name”。请尽快推荐我

1 个答案:

答案 0 :(得分:0)

我假设你有来自webApi的类别数组......

然后枚举类别数组...因为它是字典数组.... 然后还有一系列的dictioanries ..枚举,将给你你想要的......

for(NSDictionary *dict in categories)
    {
      for(NSDictionary * dict1 in [dict objectForKey :@"sub"])
      {
         NSString *name = [dict1 objectForKey :@"en_name"];

       }
    }