Ios 如何从目标C中的NSDictionary中获取键值?
我有一本字典:Ios 如何从目标C中的NSDictionary中获取键值?,ios,objective-c,Ios,Objective C,我有一本字典: { "FirstName" : "Katie", "SecondName" : "Brown" } 如何从上述字典中获取键值“FirstName”和“SecondName”您可以通过以下方式获得 if ([yourDictname objectForKey:@"FirstName"] && [yourDictname objectForKey:@"SecondName"]) { // key exists. } else { // ...
{
"FirstName" : "Katie",
"SecondName" : "Brown"
}
如何从上述字典中获取键值“FirstName”和“SecondName”您可以通过以下方式获得
if ([yourDictname objectForKey:@"FirstName"] && [yourDictname objectForKey:@"SecondName"]) {
// key exists.
}
else
{
// ...
}
或者你可以
if ([[yourDictname allKeys] containsObject:@"FirstName"] && [[yourDictname allKeys] containsObject:@"SecondName"]) {
// key exists.
}
请尝试以下代码:
NSData* yourJSONData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:yourJSONData options:0 error:nil];
NSArray *allTheKeys = [jsonObject allKeys];
您可以从字典中获取密钥数组,如
NSDictionary *dic; // your dictionary here
NSArray *allKeys = [dic allKeys];
NSLog(@"all keys : %@",allKeys);
您也可以为值获得相同的值,如[dic allValues]
NSString *jsonString = @" {\"FirstName\" : \"Katie\",\"SecondName\" : \"Brown\"}";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
for (NSString *key in dic.allKeys) {
NSLog(@"key: %@ value: %@", key, dic[key]);
}
或者重试。不显示为有效的JSONif([dict objectForKey:@“Firstname”!=nil){//key not null}可以使用NSArray*arrKey=[dict allkey];NSArray*arrVal=[dict allValues]@Shubhank我没有从谷歌找到答案,请对你的答案进行解释