iCodes
iCodes

Reputation: 1457

How to create model classes using JSONModel?

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?

Upvotes: 0

Views: 4554

Answers (2)

Kaan Dedeoglu
Kaan Dedeoglu

Reputation: 14851

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.

Upvotes: 2

Moonwalker
Moonwalker

Reputation: 3812

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

Upvotes: 0

Related Questions