I have a Dictionary:
{
"FirstName" : "Katie",
"SecondName" : "Brown"
}
How to get the key values "FirstName" and "SecondName" from the above dictionary
I have a Dictionary:
{
"FirstName" : "Katie",
"SecondName" : "Brown"
}
How to get the key values "FirstName" and "SecondName" from the above dictionary
you can get as
if ([yourDictname objectForKey:@"FirstName"] && [yourDictname objectForKey:@"SecondName"]) {
// key exists.
}
else
{
// ...
}
or you can get
if ([[yourDictname allKeys] containsObject:@"FirstName"] && [[yourDictname allKeys] containsObject:@"SecondName"]) {
// key exists.
}
Try this code:
NSData* yourJSONData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:yourJSONData options:0 error:nil];
NSArray *allTheKeys = [jsonObject allKeys];
You can get array of keys from dictionary like,
NSDictionary *dic; // your dictionary here
NSArray *allKeys = [dic allKeys];
NSLog(@"all keys : %@",allKeys);
You can got same for value also like [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]);
}
or try JSONModel.