0

我正在尝试使用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,"ava‌​ilable":true},{"fare":1200,"commission":null,"bookedBy":null,"ac":false,"availabl‌​e":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);

如何正确使用?

4

2 回答 2

2

+(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 页面提供了大量的解释。

于 2014-08-12T11:08:48.823 回答
0
  1. 您需要更正 JSON 字符串 - 删除最后一个席位后的逗号。
  2. 我认为您可能必须在此处表明您正在使用嵌套模型,即席位是您的顶级模型的嵌套模型,此时它是匿名的,但实际上由顶级 {} 大括号表示。

看看这个 - 它描述了如何使用嵌套模型进行设置。

涵盖嵌套模型的 JSONModel 教程

这是您更正后的 JSON 字符串:

{
"boardingPoints": null,
"inventoryType": 0,
"apiStatus": {
    "success": true,
    "message": "‌​SUCCESS"
},
"seats": [
    {
        "fare": 1200,
        "commission": null,
        "bookedBy": null,
        "ac": false,
        "ava‌​ilable": true
    },
    {
        "fare": 1200,
        "commission": null,
        "bookedBy": null,
        "ac": false,
        "availabl‌​e": true
    }
]}
于 2014-08-12T11:06:42.043 回答