0

I have a fairly basic problem with the JSONModel. Let's say I have the following JSON:

{"items": [
    {
        "id": 1, 
        "title": "Bla",
        "category": 1
    }
 ]} 

and this one:

{"categories": [
    {
        "id": 1, 
        "name": "Category"
    }
 ]} 

Now the easiest thing would be to put the categories inside the items and have JSONModel just use that. But there might be hundreds of items which share just a few categories, and the categories have several attributes like description, URLs and stuff, and that would blow up the items JSON.

How would I combine them in the best way using JSONModel (or might another library be better)?

My models currently look like this:

@interface Item : JSONModel

@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* title;
@property (strong, nonatomic) Category* category;

@end

@interface Category : JSONModel

@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* name;

@end
webjunkie
  • 6,891
  • 7
  • 46
  • 43
  • I don't know JSONModel, but is it an option to put the ``item`` into the ``category`` instead? Your example is a typical composition (a.k.a. containment) relationship. – Waog Aug 14 '14 at 17:31

1 Answers1

0

try this

@protocol Item
@end

@interface Item : JSONModel
@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* title;
@property (strong, nonatomic) Category* category;
@end

@interface Items : JSONModel
@property (strong, nonatomic) NSArray<Item> *items;
@end

@protocol Category
@end

@interface Category : JSONModel

@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString *name;
@end

@interface Categories : JSONModel
@property (strong, nonatomic) NSArray<Category> *categories;
@end

Your JSON is array of items or categories

Quver
  • 1,408
  • 14
  • 21