如何使用JSONModel创建模型类?

时间:2014-08-12 09:36:47

标签: ios objective-c nsdictionary nsjsonserialization jsonmodel

我正在尝试使用JSONModel创建模型类。使用NSJSONSerialization后我的json Dictionary如下所示。

      {
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);

如何正确使用?

2 个答案:

答案 0 :(得分:2)

首先,如果您的属性名称与字段名称匹配,则不需要覆盖+(JSONKeyMapper*)keyMapper。尝试为可以为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

更进一步,您可以像这样在顶级课程中进行级联:

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

答案 1 :(得分:0)

  1. 您需要更正您的JSON字符串 - 在最后一次输入座位后删除逗号。
  2. 我想你可能必须在这里表明你正在使用嵌套模型,即座位是顶层模型的嵌套模型,此时这些模型是匿名的,但实际上用顶级{}括号表示。
  3. 看看这个 - 它描述了如何使用嵌套模型进行设置。

    JSONModel tutorial covering nested models

    以下是您更正的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
        }
    ]}