问题描述
我正在尝试使用 JSONModel 创建模型类.使用NSJSONSerialization
后,我的json字典看起来如下所示.
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;
},
);
}
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},]}
我有一个像这样的模型类:-
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]'
我正在这样使用它:
//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);
如何正确使用它?
推荐答案
首先,如果属性名称与字段名称匹配,则无需覆盖+(JSONKeyMapper*)keyMapper
.尝试将可选关键字放在可以为空的字段中.
First of all, you don't need to override +(JSONKeyMapper*)keyMapper
if your property names matches with the field names. Try putting the optional keyword for fields that can be null.
@interface Seat : JSONModel
@protocol Seat;
@property (nonatomic,strong)NSString *ac;
@property (nonatomic,strong)NSString<Optional> *available;
@property(nonatomic,strong)NSString<Optional> *bookedBy;
@property(nonatomic,strong)NSString *comission;
@property(nonatomic)NSNumber * fare;
@end
再进一步,您可以像这样在顶级课程中进行级联:
Taking this a step further, you can do cascading in your top class like this:
//Result.h
#import "Seat.h"
#import "APIStatus.h"
@interface Result: JSONModel
@property(nonatomic,strong) APIStatus *apiStatus;
@property(nonatomic,strong) NSString<Optional> *boardingPoints;
@property(nonatomic,strong) NSArray<Seat> *seats;
@property(nonatomic,strong) NSNumber *boardingPoints;
@end
//APIStatus.h
@interface APIStatus: JSONModel
@property(nonatomic,strong) NSString *message;
@property(nonatomic,strong) NSNumber *success;
@end
编辑:这样,您只需要使用JSONModel初始化Result模型,所有中间类都将自动创建.您可能需要尝试使用属性类型.如果需要参考,JSONModel的github页面提供了大量解释.
EDIT: This way you only need to init the Result model with JSONModel, all the intermediary classes will be created automatically. You may need to play around with the property types. JSONModel's github page offers good amount of explanations if you need a reference.
这篇关于如何使用JSONModel创建模型类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!