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

I know that it is impossible to set multiple values ​​for the same key if the values ​​are not stored in the array.

I have an NSArray called friMainDicArrayJSON objects:

{
        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" = "";
    }
}

I save every JSON object given the key value start_time, but the problem is with the key 20:00. I would like to save 20:00as a key, and the JSON object as values. The problem is that the last 3 JSON objects contain the same key 20:00, so I would like to save these 3 JSON objects as an NSArray and set it for the key 20:00.

I would like to achieve something like this:

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

, , start_time , .

- ?

+4
3

, 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];
    }
}
+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

JSON , t21 . , , , - , :

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 operators, KVO accessors .., 0(n) .

+1
source