迭代两层NSDictionary

时间:2017-06-30 01:17:31

标签: ios objective-c nsdictionary

我正在尝试从序列化的JSON文件中遍历NSDictionary。字典有两个级别,我似乎无法正确访问第二层。

我想知道是否有办法增加objectForKey,例如:

for (id key in [itemList allKeys]) {
        [objectivesArray addObject:[[itemList objectForKey:@"Example0"] objectForKey:@"Objective"]];
    }

词典中的我的键(第二级)类似于Example0,Example1,Example2。

更新:

所需的输出将是顶级键中包含的每个值的数组。 JSON基本上是这样的: 顶级

  Example1
    Key1: Value1
    Key2: Value2
  Example2
    Key1: Value1
    Key2: Value2

等等。我已经尝试嵌套For循环来获取Key和值:

for (id key in [itemList allKeys]) {
  id value = [itemList objectForKey:key]; 
  Value = [value objectForKey:@"Example1"];
   firstLevelDictionary = [itemList objectForKey:key];

        for (id key2 in firstLevelDictionary) {
             NSLog(@"%@", [firstLevelDictionary objectForKey:@"Key1"]);
             [keyArray addObject:[firstLevelDictionary objectForKey:@"Key1"]];
       }
    }

等。

现在,当我手动将它们全部写出来时,我可以访问它们:

[key1Array addObject:[[itemList objectForKey:@"Key1"] objectForKey:@"Value1"]];
[key2Array addObject:[[itemList objectForKey:@"Key2"] objectForKey:@"Value2"]];

但这显然不是我想要的。所以我想知道是否有办法通过递增objectForKey键值中包含的整数来进行嵌套循环。

更新2:这是我正在使用的JSON格式,我现在用棒球术语代替数据: {
      " Situ0":{
         "情况":"在第一个和#34;上结合赛跑者,          "底座与#34;:[
            1          ]          "前前后后":0,          "得分":[
            1,             2          ]          "目的":" Sac Bunt",          "目的":"腥黑穗病"       },       " Situ1":{
         "情况":"在2号和#34;上结合跑步者,          "底座与#34;:[
            2          ]          "前前后后":1,          "得分":[
            1,             2          ]          "目的":"得分/移动赛跑者",          "目标":"击中Runner / Hit"       }

2 个答案:

答案 0 :(得分:0)

这可以帮助您迭代class="table table-hover"的第二层:

NSDictionary

要测试,这里有一些示例数据:

- (void)iterateSecondLayerOfDictionary:(NSDictionary *)dict {

    for (id subDictKey in dict.allKeys) {

        NSDictionary *subDict = [dict objectForKey:subDictKey];

        for (id valueKey in subDict.allKeys) {

            NSString *value = [subDict objectForKey:valueKey];

            NSLog(@"%@", value);

        }
    }
}

输出:

NSDictionary *subDict = @{ @"key1": @"value1",
                           @"key2": @"value2",
                           @"key3": @"value3",
                           @"key4": @"value4"
                         };

NSDictionary *dict = @{ @"Example0": subDict,
                        @"Example1": subDict,
                        @"Example2": subDict
                      };

NSLog(@"%@", dict);
[self iterateSecondLayerOfDictionary:dict];

答案 1 :(得分:0)

我认为这就是你所需要的。但我强烈建议不要遵循此方法。

提示:不要将subDict保留在另一个字典中,而是尝试将其放入数组(来自api)。然后,您可以通过NSArray * key1Array = [array valueForKey:@"key1"];

轻松获取它
    NSDictionary *subDict = @{ @"key0": @"value0",
                               @"key1": @"value1",
                               @"key2": @"value2",
                               };

    NSDictionary *dict = @{ @"Example0": subDict,
                            @"Example1": subDict,
                            @"Example2": subDict
                            };

    NSMutableDictionary *keyValues = [NSMutableDictionary new];
    for (int i =0; i < dict.count; i ++) {
        NSString *key = [NSString stringWithFormat:@"Example%d",i];
        NSDictionary *innerDic = [dict valueForKey:key];
        NSMutableArray *array = [NSMutableArray new];
        NSString *innerKey ;
        for (int j = 0; j < innerDic.count; j ++) {
            innerKey = [NSString stringWithFormat:@"key%d",i];
            NSString *value = [innerDic valueForKey:innerKey];
            [array addObject:value];
        }
        [keyValues setObject:array forKey:innerKey];
    }
    NSLog(@"KEY VALUES: %@", keyValues);

通过这个,你可以通过

获得key1Array
key1Array = [keyValues valueForKey: @"key1"];