我有这个名为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]; }}总结
以上是内存溢出为你收集整理的ios – 给定NSArray的JSON对象,如何在NSDictionary中为同一个键存储多个值?全部内容,希望文章能够帮你解决ios – 给定NSArray的JSON对象,如何在NSDictionary中为同一个键存储多个值?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)