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];