How to create model classes using JSONModel?

我怕爱的太早我们不能终老 提交于 2019-12-11 16:26:46

问题


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;


    },

  );
}

The JSON looks like this:

   {"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},]}

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]'

I am using it like this:

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

How to use it correctly?


回答1:


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

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.




回答2:


  1. You need to correct your JSON string - remove the comma after the last entry of seats.
  2. I think you may have to indicate you are working with nested models here i.e. seats are a nested model of your top level model which at this point is anonymous but really denoted by the top level { } braces.

Have a look at this - it describes how to set things up with nested models.

JSONModel tutorial covering nested models

Here is your corrected JSON string:

{
"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
    }
]}


来源:https://stackoverflow.com/questions/25260961/how-to-create-model-classes-using-jsonmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!