在给定NSArray的JSON对象的情况下,如何在NSDictionary中为同一个键存储多个值?

时间:2015-02-15 01:50:33

标签: ios objective-c json nsarray nsdictionary

我知道不可能为同一个键设置多个值,除非这些值存储在一个数组中。

我有一个名为friMainDicArray的JSON对象的NSArray:

{
        day = 0;
        "end_time" = "17:30";
        "english_event" = "Lion Dance";
        "english_performer" = "Legendary Group";
        "image_link" = "schedule_miss_vn";
        stage = 0;
        "start_time" = "17:00";
        "viet_event" = "<null>";
        "viet_performer" = "Nh?m Legendary";
    },
        {
        day = 0;
        "end_time" = "18:00";
        "english_event" = Singing;
        "english_performer" = "Ivan Cheong";
        "image_link" = "schedule_miss_vn";
        stage = 0;
        "start_time" = "17:30";
        "viet_event" = "Ca Nh?c";
        "viet_performer" = "Ivan Cheong";
    },
        {
        day = 0;
        "end_time" = "22:00";
        "english_event" = Singing;
        "english_performer" = "Between California and Summer";
        "image_link" = "";
        stage = 0;
        "start_time" = "21:00";
        "viet_event" = "Ca Nh?c";
        "viet_performer" = "";
    },
        {
        day = 0;
        "end_time" = "";
        "english_event" = "End of Day";
        "english_performer" = "";
        "image_link" = "";
        stage = 0;
        "start_time" = "22:00";
        "viet_event" = "";
        "viet_performer" = "";
    },
        {
        day = 0;
        "end_time" = "21:00";
        "english_event" = Music;
        "english_performer" = "DJ Happee From Channel 93.3";
        "image_link" = "";
        stage = 0;
        "start_time" = "20:00";
        "viet_event" = "";
        "viet_performer" = "";
    },
        {
        day = 0;
        "end_time" = "21:00";
        "english_event" = Music;
        "english_performer" = "Adam Cease";
        "image_link" = "";
        stage = 0;
        "start_time" = "20:00";
        "viet_event" = "";
        "viet_performer" = "";
    },
        {
        day = 0;
        "end_time" = "21:00";
        "english_event" = "Ao Dai Fashion Show";
        "english_performer" = "";
        "image_link" = "";
        stage = 0;
        "start_time" = "20:00";
        "viet_event" = "";
        "viet_performer" = "";
    }
}

我在给定start_time键值的情况下存储每个JSON对象,但问题在于20:00键。我想将20:00作为键存储,将JSON对象存储为值。问题是,最后3个JSON对象包含相同的20:00键,因此我想将这3个JSON对象存储为NSArray,并将其设置为20:00键。

我想实现这样的目标:

for (int i = 0; i < [friMainDicArray count]; i++)
{
    [friMainDic setValue:friMainDicArray[i] forKey:[friMainDicArray[i] valueForKey:@"start_time" ] ]
}

但是,我并不确切地知道检查start_time键是否相同的逻辑,然后将其值添加到同一个键中。

有人能以正确的方式指出我吗?

3 个答案:

答案 0 :(得分:2)

我认为您可以循环数组,在setValueForKey之前,检查密钥是否已经存在,如果是,并且该密钥的值是NSDictionary类型,则将当前密钥与现有密钥组合为数组,否则该键的值应为NSArray,然后将当前项添加到该数组。

for (NSDictionary *item in friMainDicArray)
{
    NSString *key = [item valueForKey:@"start_time"];
    if ([friMainDic objectForKey:key]) {
        id value = [friMainDic objectForKey:key];
        if ([value isKindOfClass:[NSArray class]]) {
            NSMutableArray *array = [NSMutableArray arrayWithArray:(NSArray*)value];
            [array addObject:item];
            [friMainDic setValue:array forKey:key];
        } else {
            NSDictionary *dict = (NSDictionary*)value;
            NSArray *array = @[dict,item];
            [friMainDic setValue:array forKey:key];
        }
    } else {
        [friMainDic setValue:item forKey:key];
    }
}

答案 1 :(得分:2)

嗯,我想我会像这样接近它。找出那里是否有阵列。制作一个可变版本并添加到它。将其重新设置为字典。如果没有创建数组并添加它。

for (int i = 0; i < [friMainDicArray count]; i++)
{
    if (firMainDic[[friMainDicArray[i] valueForKey:@"start_time" ])
    {
        //pull out the array and add to it
        NSMutableArray *mutableArray = [[NSMutableArray alloc]initWithArray: firMainDic[[friMainDicArray[i] valueForKey:@"start_time" ]]];
        [mutableArray addObject:friMainDicArray[i]];
        [friMainDic setValue:mutableArray forKey:[friMainDicArray[i] valueForKey:@"start_time" ] ];
    }
    else
    {
        NSArray *array = @[friMainDicArray[i]];
        [friMainDic setValue:array forKey:[friMainDicArray[i] valueForKey:@"start_time" ] ]
    }
}

不是100%肯定,但我相信当你把一个可变数组放入一个字典时,它就会变成一个非可变数组。无论哪种方式,这至少应该让你开始朝着正确的方向前进。

答案 2 :(得分:1)

我建议您始终将JSON个对象存储在数组中,无论keys是相同还是不同。通过这种方式,您至少可以避免在每次迭代中进行某种模糊的检查,例如;

if ([dict[key] isKindOfClass:[NSMutableArray class]]) {
    NSMutableArray *values = (NSMutableArray *)dict[key];
    [values addObject:newObject];
}
else {
    NSMutableArray *values = [NSMutableArray arrayWithObject:dict[key]];
    [values addObject:newObject];
}

相反,我会通过使用以下方法迭代数组来填充结果字典:( 假设所有JSON对象现在都是NSDictionary实例

NSArray *events = @[@{@"start_time": @"17:00", @"end_time": @"17:30", @"english_event": @"Lion Dance", @"english_performer": @"Legendary Group"},
                    @{@"start_time": @"17:30", @"end_time": @"18:00", @"english_event": @"Singing", @"english_performer": @"Ivan Cheong"},
                    @{@"start_time": @"20:00", @"end_time": @"21:00", @"english_event": @"Music", @"english_performer": @"Adam Cease"},
                    @{@"start_time": @"20:00", @"end_time": @"21:00", @"english_event": @"Music", @"english_performer": @"DJ Happee From Channel 93.3"},
                    @{@"start_time": @"21:00", @"end_time": @"22:00", @"english_event": @"Singing", @"english_performer": @"Between California and Summer"}];

NSMutableDictionary *resultDict = [NSMutableDictionary dictionary];

[events enumerateObjectsUsingBlock:^(NSDictionary *event, NSUInteger idx, BOOL *stop) {

    NSMutableArray *values = resultDict[event[@"start_time"]];

    if (values == nil) {
        values = [[NSMutableArray alloc] init];
        resultDict[event[@"start_time"]] = values;
    }
    [values addObject:event];
}];

注意:我找不到更短的方式,包括collection operatorsKVO accessors等,实际上需要0(n)才能对字典进行分组。< / p>