我确信这个问题在这里被问过几十次,但我似乎找不到任何与我需要的东西相近的东西。我有一个问题,这是正确的。我需要从NSDictionary
中获取值并将其放入NSArray
。
NSDictionary *dictionary = @{@"ID1":@{
@"mainPhone": @"<number>",
@"Address": @"<address>",
@"City": @"<Tulsa, OK>",
@"Date": @"1-1-2017",
@"Person1": @{
@"First": @"<FirstName>",
@"Last": @"<LastName>",
@"Phone": @"<number>",
@"Email": @"email@gmail.com",
@"Birth": @"<date>",
@"ID": @"12345",
},
@"Person2": @{
@"First": @"<FirstName>",
@"Last": @"<LastName>",
@"Phone": @"<number>",
@"Email": @"email@gmail.com",
@"Birth": @"<date>",
@"ID": @"12345",
},
@"ID2":@{
@"mainPhone": @"<number>",
@"Address": @"<address>",
@"City": @"<Tulsa, OK>",
@"Date": @"1-1-2017",
@"Person3": @{
@"First": @"<FirstName>",
@"Last": @"<LastName>",
@"Phone": @"<number>",
@"Email": @"email@gmail.com",
@"Birth": @"<date>",
@"ID": @"12345",
},
@"Person4": @{
@"First": @"<FirstName>",
@"Last": @"<LastName>",
@"Phone": @"<number>",
@"Email": @"email@gmail.com",
@"Birth": @"<date>",
@"ID": @"12345",
},
};
在上面的示例词典中,我需要在第3层字典下获取密钥First
并将其加载到数组中。标记为ID
和Person
的词典将被分配单独的ID号,这将是唯一不会是静态的键。提前谢谢。
答案 0 :(得分:0)
为此你可以循环遍历字典,然后第一个idKey然后访问该字典并通过循环访问它的详细信息。
for (NSString *key in dictionary) {
NSDictionary *idDic = [dictionary objectForKey:key];
for (NSString *idDicKey in idDic) {
if ([[idDic objectForKey:idDicKey] isKindOfClass:[NSDictionary class]]) {
NSString *firstName = [dictionary valueForKeyPath:[NSString stringWithFormat:@"%@.%@.First",key, idDicKey]];
NSLog(@"%@",firstName);
}
}
}