I know it's not possible to set multiple values for the same key, unless the values are stored in an array.
I have this NSArray called friMainDicArray
of JSON 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 am storing each JSON object, given the start_time
key value, but the problem is with the 20:00
key. I would like to store the 20:00
as the key, and the JSON object as the values. The problem is, the last 3 JSON objects contain the same 20:00
key, so I would like to store those 3 JSON objects as an NSArray, and set it for the 20:00
key.
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" ] ]
}
However, I don't know exactly the logic to check if the start_time
key is the same, then add its value to the same key.
Can someone point me in the right way?
JSON array can store multiple values. It can store string, number, boolean or object in JSON array. In JSON array, values must be separated by comma. The [ (square bracket) represents JSON array.
var myMap = new Map(); myMap. set("1",["A","B"]); Use an array of values, like above, in order to have a Map use a the key "1" to map to multiple value. The second call to set in the OP, will overwrite the first value.
NSDictionary creates static dictionaries, and NSMutableDictionary creates dynamic dictionaries. (For convenience, the term dictionary refers to any instance of one of these classes without specifying its exact class membership.) A key-value pair within a dictionary is called an entry.
I think you can loop the array and before setValueForKey, you check if the key already exists, if it is yes, and if value for that key is of type NSDictionary, you combine the current one with the existing one as an array, otherwise value for that key should be a NSArray, you then add the current item to that array.
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];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With