JSON is a very simple format for HTTP transaction between app and backend server. Currently, most of public API return data in JSON format, such as Google API. In IOS, there are several ways to convert the JSON String into Object-C object. Usually, the JSON data will be converted into NSDictionary object. Here are two pieces of example source code to convert JSON data into object in IOS.
The Simplest Way to Convert JSON String into Object in IOS
The most simple way to convert JSON into Object in IOS is calling the NSString function objectFromJSONString. Here is the example source code:
//jsonStr is the json string NSDictionary *dict = [jsonStr objectFromJSONString];
Convert JSON String into NSData and NSDictionary
Here is another way to convert JSON string into NSDictionary. This way is more complicated than the simplest way. We will convert the JSON string into NSData, then we call NSJSONSerialization’s function JSONObjectWithData to convert NSData into NSDictionary.
NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding]; NSError *error; NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
Can’t find any NSString method ‘objectFromJSONString’. Please provide the reference where you find it.
You need to use JSONKIt