Parsing JSON to a predefined class in Objective C
Instead of using dictionaries directly you can always deserialize (parse) JSON to your class with using Key-value coding. Key-value coding is a great feature of Cocoa that lets you access properties and instance variables of class at runtime by name. As I can see your JSON model is not complex and you can apply this easily.
person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property NSString *personName;
@property NSString *personMiddleName;
@property NSString *personLastname;
- (instancetype)initWithJSONString:(NSString *)JSONString;
@end
person.m
#import "Person.h"
@implementation Person
- (instancetype)init
{
self = [super init];
if (self) {
}
return self;
}
- (instancetype)initWithJSONString:(NSString *)JSONString
{
self = [super init];
if (self) {
NSError *error = nil;
NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:JSONData options:0 error:&error];
if (!error && JSONDictionary) {
//Loop method
for (NSString* key in JSONDictionary) {
[self setValue:[JSONDictionary valueForKey:key] forKey:key];
}
// Instead of Loop method you can also use:
// thanks @sapi for good catch and warning.
// [self setValuesForKeysWithDictionary:JSONDictionary];
}
}
return self;
}
@end
appDelegate.m
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// JSON String
NSString *JSONStr = @"{ \"personName\":\"MyName\", \"personMiddleName\":\"MyMiddleName\", \"personLastname\":\"MyLastName\" }";
// Init custom class
Person *person = [[Person alloc] initWithJSONString:JSONStr];
// Here we can print out all of custom object properties.
NSLog(@"%@", person.personName); //Print MyName
NSLog(@"%@", person.personMiddleName); //Print MyMiddleName
NSLog(@"%@", person.personLastname); //Print MyLastName
}
@end
The article using JSON to load Objective-C objects good point to start.
We can automatically map response json in our model classes with the help of third party library
JSONMode https://github.com/mattiaslevin/ObjectMapper
Simply create your class like below define your data model properties. Be careful the keys of your json response should be same as the property you are defining. You will get more clear picture below because I am sharing my json response with the structure of the model classes -
Response json
{
"code": 200,
"data": [
{
"stockName": "sh000001",
"category": "china",
"stockStatus": "open",
"roadMap": [
{
"stockTimeStamp": "10:25",
"stockValue": "2789.915",
"number1": 1,
"number2": 5
},
{
"stockTimeStamp": "10:30",
"stockValue": "2790.153",
"number1": 5,
"number2": 3
}
],
"gameData": [
{
"gameUUID": "e4fcd001-2499-45c3-a21c-d573b9e378cc",
"gameStatus": "Open"
}
]
},
{
"stockName": "usindex",
"category": "usa",
"stockStatus": "open",
"roadMap": [
{
"stockTimeStamp": "10:20",
"stockValue": "100.1020",
"number1": 2,
"number2": 0
},
{
"stockTimeStamp": "10:25",
"stockValue": "100.0958",
"number1": 5,
"number2": 8
}
],
"gameData": [
{
"gameUUID": "1a6c9889-41e9-410a-a409-e10126ffeeb5",
"gameStatus": "Open"
}
]
},
{
"stockName": "btc1",
"category": "crypto",
"stockStatus": "open",
"roadMap": [
{
"stockTimeStamp": "02:26",
"stockValue": "7670.00",
"number1": 0,
"number2": 0
},
{
"stockTimeStamp": "02:25",
"stockValue": "7670.00",
"number1": 0,
"number2": 0
}
],
"gameData": [
{
"gameUUID": "40526121-f199-4649-b169-9913bd883186",
"gameStatus": "Open"
}
]
}
],
"status": true,
"message": [
"success"
]
}
YourModel.h file
#import "JSONModel.h"
@interface RoadmapElementModel : JSONModel
@property (nonatomic) NSInteger number1;
@property (nonatomic) NSInteger number2;
@property (nonatomic) NSString *stockTimeStamp;
@property (nonatomic) NSString *stockValue;
@end
@interface RoadmapDataModel : JSONModel
@property (nonatomic) NSString *stockName;
@property (nonatomic) NSString *category;
@property (nonatomic) NSString *stockStatus;
@property (nonatomic) NSArray <RoadmapElementModel *> *roadMap;
@end
@interface RoadMapModel : JSONModel
@property (nonatomic) NSInteger code;
@property (nonatomic) BOOL status;
@property (nonatomic) NSArray<RoadmapDataModel *>*data;
@property (nonatomic) NSArray<NSString *> *message;
@end
Note:- You do not need to write anything to your YourModel.h
file.
Now just write below code after getting the response JsonString in your ViewController class
NSError *error;
RoadMapModel *roadmap = [[RoadMapModel alloc] initWithString:myString error:&error];
- (id)initWithDictionary:(NSDictionary *)dictionary {
if (self = [super init]) {
_a = [dictionary objectForKey:@"a"];
_b = [dictionary objectForKey:@"b"];
_c = [dictionary objectForKey:@"c"];
}
return self;
}