我正在尝试使用JSONModel创建模型类。使用后的我的json 字典NSJSONSerialization
如下所示。
{
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;
},
);
}
JSON 看起来像这样:
{"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},]}
我有一个像这样的模型类:-
@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;
对于映射键,我这样做了:-
+(JSONKeyMapper*)keyMapper {
return [[JSONKeyMapper alloc] initWithDictionary:@{@"ac":@"ac",
@"available":@"available",
@"bookedBy":@"bookedBy",
@"commission":@"commission",
@"fare":@"fare", }];
}
但是,当我尝试使用此模型时,出现以下错误:
[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]'
我这样使用它:
//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);
如何正确使用?