如何使用 JSONModel 创建模型类?
Posted
技术标签:
【中文标题】如何使用 JSONModel 创建模型类?【英文标题】:How to create model classes using JSONModel? 【发布时间】:2014-08-12 09:36:47 【问题描述】:我正在尝试使用JSONModel 创建模型类。使用NSJSONSerialization
后的我的json 字典如下所示。
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);
如何正确使用?
【问题讨论】:
您的 JSON 格式未通过 JSONLint - 纠正它,我会帮助您 "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,"可用":true,] 【参考方案1】:首先,如果您的属性名称与字段名称匹配,则无需覆盖 +(JSONKeyMapper*)keyMapper
。尝试为可以为空的字段添加可选关键字。
@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
更进一步,您可以像这样在***类中进行级联:
//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 页面提供了大量的解释。
【讨论】:
好的...那么如何实现这个结果模型类呢? :-] 好的....而不是结果,我只是将其更改为“SeatResult”....对于初始化,我确实这样做了.... seatResult = [[SeatResult alloc]initWithData:responseData 错误:&json错误]; NSLog(@"座位数组:%@",seatResult.seats); ....及其显示数据...感谢 KAAN ..虽然我仍然需要访问数组以查看如何访问它。 很高兴它有帮助,如果它解决了您最初的问题,请接受答案。【参考方案2】:-
您需要更正 JSON 字符串 - 删除最后输入席位后的逗号。
我认为您可能必须在此处表明您正在使用嵌套模型,即席位是您的***模型的嵌套模型,此时它是匿名的,但实际上由*** 大括号表示。
看看这个 - 它描述了如何使用嵌套模型进行设置。
JSONModel tutorial covering nested models
这是您更正后的 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
]
【讨论】:
以上是关于如何使用 JSONModel 创建模型类?的主要内容,如果未能解决你的问题,请参考以下文章