NSJSONSerialization does the job of converting your JSON data into usable data structures like NSDictionary or NSArray very well. I recommend it even more because it is part of Cocoa's open interface and is supported by Apple.
However, if you want to map the contents of your JSON to your Objective-C objects, you will need to map each attribute from NSDictionary / NSArray to your object property. It can be a little painful if your objects have many attributes.
To automate the process, I recommend that you use Motis (a personal project) for NSObject to execute it, so it is very light and flexible. You can read how to use it in this post . But just to show you, you just need to define a dictionary with JSON object attribute mapping for your Objective-C object property names in NSObject subclasses:
- (NSDictionary*)mjz_motisMapping { return @{@"json_attribute_key_1" : @"class_property_name_1", @"json_attribute_key_2" : @"class_property_name_2", ... @"json_attribute_key_N" : @"class_property_name_N", }; }
and then do the parsing by doing:
- (void)parseTest { NSData *data = jsonData;
Setting properties from the dictionary is done using KeyValueCoding (KVC), and you can check each attribute before setting it using KVC check.
source share