I am trying to create Model Class by using JSONModel.My json Dictionary after using NSJSONSerialization
looks like below.
{
apiStatus = {
message = SUCCESS;
success = 1;
};
boardingPoints = "<null>";
inventoryType = 0;
seats = (
{
ac = 0;
available = 1;
bookedBy = "<null>";
commission = "<null>";
fare = 1200;
},
{
ac = 0;
available = 1;
bookedBy = "<null>";
commission = "<null>";
fare = 1200;
},
);
}
The JSON looks like this:
{"boardingPoints":null,"inventoryType":0,"apiStatus":{"success":true,"message":"SUCCESS"},"seats":[{"fare":1200,"commission":null,"bookedBy":null,"ac":false,"available":true},{"fare":1200,"commission":null,"bookedBy":null,"ac":false,"available":true},]}
I have a model class like this :-
@interface Seat : JSONModel
@property (nonatomic,strong)NSString *ac;
@property (nonatomic,strong)NSString *available;
@property(nonatomic,strong)NSString *bookedBy;
@property(nonatomic,strong)NSString *comission;
@property(nonatomic)NSNumber * fare;
For mapping keys I have done like this:-
+(JSONKeyMapper*)keyMapper {
return [[JSONKeyMapper alloc] initWithDictionary:@{@"ac":@"ac",
@"available":@"available",
@"bookedBy":@"bookedBy",
@"commission":@"commission",
@"fare":@"fare", }];
}
However when I try to use this model I get the following error:
[JSONModel.m:252] Incoming data was invalid [Seat initWithDictionary:]. Keys missing: {(
bookedBy,
comission,
available,
ac,
fare,
)}
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
I am using it like this:
//Using JSON Model.
NSError *jsonError;
seat = [[Seat alloc]initWithDictionary:jsonDictionary error:&jsonError];
jsonArray = @[seat.ac,seat.available,seat.bookedBy,seat.comission,seat.fare];
NSLog(@"JSON Model Array : %@", jsonArray);
How to use it correctly?