好吧,如果Type
將每次返回你的key
,你可以這樣做:
NSString *yourKey = [yourDictionary objectForKey:@"Type"];
NSDictionary *countDict = [yourDictionary objectForKey:yourKey];
int bird = [[countDict objectForKey:@"bird"] intValue];
int animal = [[countDict objectForKey:@"animal"] intValue];
其他解決方案:
for (NSString *aKey in yourDictionary)
{
if ([[yourDictionary objectForKey:@"aKey"] isKindOfClass:[NSDictionary class]])
{
NSDictionary *countDict = [yourDictionary objectForKey:aKey];
int bird = [[countDict objectForKey:@"bird"] intValue];
int animal = [[countDict objectForKey:@"animal"] intValue];
}
}
其他的解決方案,根據你的對象是否可能包含其他子字典:
for (NSString *aKey in yourDictionary)
{
if ([[yourDictionary objectForKey:@"aKey"] objectForKey:@"Count"])
{
NSDictionary *countDict = [[yourDictionary objectForKey:@"aKey"] objectForKey:@"Count"];
int bird = [[countDict objectForKey:@"bird"] intValue];
int animal = [[countDict objectForKey:@"animal"] intValue];
}
}
我不明白爲什麼「Type」條目存在,因爲它與頂級字典鍵相同。刪除它並依靠頂級密鑰是找到條目的最快捷方式。另外,它看起來像那些'計數'字典條目應該是數字,而不是字符串... – trojanfoe
'鍵入'是'鍵'?否則,如果您總是等待帶有'count'鍵的子字典,則可以迭代並獲取'NSDictionary'的鍵。 – Larme
@ trojanfoe.I正在客戶端工作,實際上它來自severside.This格式是需要他們的網絡開發 – user3564078