Using Objective-C JSONModel with an array of objects as entry point

2.2k views Asked by At

I got a json objects which starts with an array of objects. I wanna use JSONModel together with this json object but I can't find an example how to do this.

Here's my json:

{
  "days": [{
    "date": "2016-12-22T00:00:00.000Z",
    "items": [{ ... }]
  },{
    "date": ...
  }
}

I created a DaysModel

@interface DaysModel : JSONModel

@property (nonatomic) NSArray<DayModel *> *days;

And the corresponding DayModel (in fact, I only need an Array of "DayModel Types")

@interface DaysModel : JSONModel

@property (nonatomic) NSDate *date;
@property (nonatomic) NSArray<ItemModel *> *items;

But when initializing my Model with a string

DaysModel *myDays = [[DaysModel alloc] initWithString:teststring error:&jsonError];

The content of myDays.days will be an Array of Dictionaries instead of an Array of DayModels.

1

There are 1 answers

1
francesco.venica On BEST ANSWER

Probably you forgot to add the protocol, try to read the documentation on github?

https://github.com/jsonmodel/jsonmodel#model-collections

try to add

@protocol ItemModel;

in DaysModel

and change from:

@property (nonatomic) NSArray<ItemModel *> *items;

to:

@property (nonatomic) NSArray<ItemModel> *items;