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

How to store multiple values for the same key in an NSDictionary, given an NSArray of JSON objects?

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

我有一个名为 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" = "";
    }
}

我正在存储每个 JSON 对象,给定 start_time 键值,但问题出在 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 键是否相同,然后将其值添加到相同键的逻辑。

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

我认为你可以循环数组,在 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];
    }
}

好吧,我想我会这样处理。看看那里是否已经有一个数组。制作一个可变版本并添加到它。把它放回字典里。如果没有创建一个数组并添加到它。

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% 确定,但我相信当你将一个可变数组放入字典时,它会作为一个非可变数组出现。无论哪种方式,这至少应该让您朝着正确的方向开始。

我建议您始终将 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) 进行分组字典 .