-4

我有一个NSMutableDictionary从服务器返回的 JSON 格式。

从中我必须删除一个为空且不使用的键。我必须同时删除键和值

我们怎样才能删除那个键....

关键值是

appointmentType = 1;
description = Right;
endDate = "2013-09-11";
endTime = "02:30:00";
flgHolidaySchedule = 1;
flgSiteSetupSchedule = 1;
therapistId = "maheswaran@excelgoodies.com";
uri = "<null>";

现在我必须从字典中删除 uri Key....

4

3 回答 3

0

这真的很简单:

[yourDictionaryFromJSON removeObjectForKey:@"uri"];
于 2013-09-11T07:39:56.657 回答
0

遍历字典并查找任何空条目并将其删除。

NSMutableDictionary *prunedDictionary = [NSMutableDictionary dictionary];
for (NSString * key in [yourDictionary allKeys])
{
    if (![[yourDictionary objectForKey:key] isKindOfClass:[NSNull class]])
        [prunedDictionary addObject:[yourDictionary objectForKey:key] forKey:key];
}

之后,prunedDictionary 应该在原始字典中包含所有非空项。

于 2013-09-11T06:57:20.387 回答
0

尝试使用以下代码:

NSMutableDictionary *myTemDictionary = [NSMutableDictionary dictionary];

for (NSString * key in [yourDictionaryFromJSON allKeys])
{
    if (![[yourDictionaryFromJSON objectForKey:key] isKindOfClass:[NSNull class]])
        [myTemDictionary addObject:[yourDictionary objectForKey:key] forKey:key];
}

NSLog(@"%@", myTemDictionary); // check it in console your dictionary has not any NULL value
于 2013-09-11T06:57:27.447 回答