如何从 NSDictionary 获取值并将其移动到 NSArray
How get a value from NSDictionary and move it to an NSArray
我敢肯定这个问题已经在这里被问过几十次了,但我似乎找不到任何接近我需要的东西。我在解决这个问题时遇到了一点问题。我需要从 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 号,这将是唯一不是静态的键。提前致谢。
为此,您可以循环遍历字典,首先使用 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);
}
}
}
我敢肯定这个问题已经在这里被问过几十次了,但我似乎找不到任何接近我需要的东西。我在解决这个问题时遇到了一点问题。我需要从 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 号,这将是唯一不是静态的键。提前致谢。
为此,您可以循环遍历字典,首先使用 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);
}
}
}