Murali Krish Murali Krish - 3 months ago 8
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

Try this code:

NSData* yourJSONData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:yourJSONData options:0 error:nil];
NSArray *allTheKeys = [jsonObject allKeys];