I have a json string like:
{
\"a\":\"val1\",
\"b\":\"val2\",
\"c\":\"val3\"
}
And I have an objective C header file like:
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;
}
You have two solutions:
Manual
Write code to parse the JSON to a dictionary and after populate manually an instance of your target object
NSDictionary *jsonDictionary = //JSON parser
TestItem *ti = [TestItem new];
ti.a = [jsonDictionary objectForKey:@"a"];
ti.b = [jsonDictionary objectForKey:@"b"];
ti.c = [jsonDictionary objectForKey:@"c"];
iOS provides you a json parser. Look at this reply for more infos How do I deserialize a JSON string into an NSDictionary? (For iOS 5+)
(you should also check that the objects type match your expectations and eventually manage properly cases of error)
Mapping lib
Use a mapper library like JTObjectMapping that can help you to define how your object should be filled using the JSON. Usually I prefer this solution. It automatically checks for types and your code will be clearer.
Use OCMapper to automate your mapping. it has the ability to automatically map all fields, and simple to use.
https://github.com/aryaxt/OCMapper
let request = Manager.sharedInstance.request(requestWithPath("example.com/users/5", method: .GET, parameters: nil))
request.responseObject(User.self) { request, response, user, error in
println(user.firstName)
}
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.