-1

I have a Dictionary:

{
  "FirstName" : "Katie",
  "SecondName" : "Brown"
}

How to get the key values "FirstName" and "SecondName" from the above dictionary

murali kurapati
  • 1,510
  • 18
  • 23

4 Answers4

1

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.
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

Try this code:

NSData* yourJSONData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:yourJSONData options:0 error:nil];
NSArray *allTheKeys = [jsonObject allKeys];
Arun
  • 1,391
  • 1
  • 10
  • 29
Bhadresh Kathiriya
  • 3,147
  • 2
  • 21
  • 41
1

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]

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0
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.