在 NSDictionary 中查找子键
Find sub key in NSDictionary
我有一个很大的 NSDictionary。外汇
"m:GetFolderResponse" = {
"m:ResponseMessages" = {
"m:GetFolderResponseMessage" = {
ResponseClass = Success;
"m:Folders" = {
"t:CalendarFolder" = {
"t:ChildFolderCount" = {
text = 0;
};
"t:DisplayName" = {
text = Calendar;
};
"t:FolderId" = {
ChangeKey = "AgAAABYAAABGewbOYWpKSrW/k23iIoFkAPJWd7/8";
Id = "AAMkADkwOWE2NjEyLTMwZWQtNGYyMy05OGQ1LWZjZjFkZGY5MTBhMAAuAAAAAAC1cjo8jkv5SKjQt5WaSmd1AQBGewbOYWpKSrW/k23iIoFkAPJWc0NrAAA=";
};
};
};
"m:ResponseCode" = {
text = NoError;
};
};
};
"xmlns:m" = "http://schemas.microsoft.com/exchange/services/2006/messages";
"xmlns:t" = "http://schemas.microsoft.com/exchange/services/2006/types";
};
}
您可能已经猜到了,m:Folders 中可以有多个。因此我想找到 m:Folders child,其中 t:DisplayName 等于一个变量值。我该怎么做?
- (void)filterMutableDictionary:(NSDictionary*)aDictionary andKeyName:(NSString *)keyName
{
if ([keyName isEqualToString:@"t:CalendarFolder"]) {
if ([[[aDictionary objectForKey:@"t:DisplayName"] objectForKey:@"text"] isEqualToString:searchCalendarName]) {
NSDictionary *temp = [aDictionary objectForKey:@"t:FolderId"];
CalID = [temp objectForKey:@"Id"];
CalChangeID = [temp objectForKey:@"ChangeKey"];
}
}
// enumerate key/values, filtering appropriately, recursing as necessary
NSLog(@"%@",aDictionary);
[aDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
if ([value isKindOfClass: [NSMutableDictionary class]] || [value isKindOfClass: [NSDictionary class]]) {
[self filterMutableDictionary: value andKeyName:key];
}
}];
}
我有一个很大的 NSDictionary。外汇
"m:GetFolderResponse" = {
"m:ResponseMessages" = {
"m:GetFolderResponseMessage" = {
ResponseClass = Success;
"m:Folders" = {
"t:CalendarFolder" = {
"t:ChildFolderCount" = {
text = 0;
};
"t:DisplayName" = {
text = Calendar;
};
"t:FolderId" = {
ChangeKey = "AgAAABYAAABGewbOYWpKSrW/k23iIoFkAPJWd7/8";
Id = "AAMkADkwOWE2NjEyLTMwZWQtNGYyMy05OGQ1LWZjZjFkZGY5MTBhMAAuAAAAAAC1cjo8jkv5SKjQt5WaSmd1AQBGewbOYWpKSrW/k23iIoFkAPJWc0NrAAA=";
};
};
};
"m:ResponseCode" = {
text = NoError;
};
};
};
"xmlns:m" = "http://schemas.microsoft.com/exchange/services/2006/messages";
"xmlns:t" = "http://schemas.microsoft.com/exchange/services/2006/types";
};
}
您可能已经猜到了,m:Folders 中可以有多个。因此我想找到 m:Folders child,其中 t:DisplayName 等于一个变量值。我该怎么做?
- (void)filterMutableDictionary:(NSDictionary*)aDictionary andKeyName:(NSString *)keyName
{
if ([keyName isEqualToString:@"t:CalendarFolder"]) {
if ([[[aDictionary objectForKey:@"t:DisplayName"] objectForKey:@"text"] isEqualToString:searchCalendarName]) {
NSDictionary *temp = [aDictionary objectForKey:@"t:FolderId"];
CalID = [temp objectForKey:@"Id"];
CalChangeID = [temp objectForKey:@"ChangeKey"];
}
}
// enumerate key/values, filtering appropriately, recursing as necessary
NSLog(@"%@",aDictionary);
[aDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
if ([value isKindOfClass: [NSMutableDictionary class]] || [value isKindOfClass: [NSDictionary class]]) {
[self filterMutableDictionary: value andKeyName:key];
}
}];
}