Murali Krish Murali Krish - 1 year ago 123
Objective-C Question

How to get the key name from JSON String in Objective C?

I have a JSON String like this:

"FirstName" : "Murali",
"SecondName" : "Krish"

I don't know that the response contains the keys FirstName and SecondName. How can one find that the JSON string has the keys "FirstName" and "SecondName"?

Answer Source

Try this code:

NSData* yourJSONData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:yourJSONData options:0 error:nil];
NSArray *allTheKeys = [jsonObject allKeys];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download