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
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
Related
I'm using the same example on the web
OrderModel.h
#protocol ProductModel
#end
#interface ProductModel : JSONModel
#property (assign, nonatomic) int id;
#property (strong, nonatomic) NSString* name;
#property (assign, nonatomic) float price;
#end
#implementation ProductModel
#end
#interface OrderModel : JSONModel
#property (assign, nonatomic) int order_id;
#property (assign, nonatomic) float total_price;
#property (strong, nonatomic) NSArray<ProductModel>* products;
#end
#implementation OrderModel
#end
But when I build the project I face one issue "Duplicate symbols"
duplicate symbol _OBJC_CLASS_$_OrderModel
ld: 576 duplicate symbols for architecture arm64
clang: error: linker command failed with exit code 1
The #implementation should be present in a .m file and the #interface in a .h file.
You should only import the .h file. Otherwise you will have multiple implementations for the same class.
ProductModel.h:
#interface ProductModel : JSONModel
#property (assign, nonatomic) int id;
#property (strong, nonatomic) NSString* name;
#property (assign, nonatomic) float price;
#end
ProductModel.m:
#import "ProductModel.h"
#implementation ProductModel
#end
OrderModel.h:
#interface OrderModel : JSONModel
#property (assign, nonatomic) int order_id;
#property (assign, nonatomic) float total_price;
#property (strong, nonatomic) NSArray<ProductModel>* products;
#end
OrderModel.m
#import "OrderModel.h"
#implementation OrderModel
#end
If you would want to use the ProductModel class in a View Controller for example just import the "ProductModel.h":
#import "ProductModel.h"
in my app i use Yahoo Api and get responce i currently use JSONModel in this create a json JSONModel class.
Past_Match.h file Like
#import <JSONModel/JSONModel.h>
#protocol squadModel #end
#interface squadModel : JSONModel
#property (assign, nonatomic) int i;
#property (strong, nonatomic) NSString *full;
#end
#interface logoModel : JSONModel
#property (strong, nonatomic) NSString *std;
#end
#protocol teamsModel #end
#interface teamsModel : JSONModel
#property (assign, nonatomic) int i;
#property (strong, nonatomic) NSString *fn;
#property (strong, nonatomic) NSString *sn;
#property (strong, nonatomic) logoModel *logo;
#property (strong, nonatomic) NSArray<squadModel> *squad;
#end
#interface aModel : JSONModel
#property (assign, nonatomic) int i;
#property (assign, nonatomic) int r;
#property (assign, nonatomic) int o;
#property (assign, nonatomic) int w;
#property (assign, nonatomic) int cr;
#end
#interface sModel : JSONModel
#property (strong, nonatomic) aModel *a;
#end
#protocol tModel #end
#interface tModel : JSONModel
#property (assign, nonatomic) int i;
#property (assign, nonatomic) int a;
//#property (strong, nonatomic) NSString *c;
//#property (strong, nonatomic) NSString *dt;
#property (assign, nonatomic) int fd;
#property (assign, nonatomic) int bd;
//#property (strong, nonatomic) NSString *cb;
#property (assign, nonatomic) int b;
#property (assign, nonatomic) int r;
#property (assign, nonatomic) int sr;
#property (assign, nonatomic) int six;
#property (assign, nonatomic) int four;
#end
#interface anewModel : JSONModel
#property (strong, nonatomic) NSArray<tModel> *t;
#end
#interface dModel : JSONModel
#property (strong, nonatomic) anewModel *a;
#end
#protocol past_ingsModel #end
#interface past_ingsModel : JSONModel
#property (strong, nonatomic) sModel *s;
#property (strong, nonatomic) dModel *d;
#end
#interface ScorecardModel : JSONModel
#property (strong, nonatomic) NSString *mid;
#property (strong, nonatomic) NSArray<teamsModel> *teams;
#property (strong, nonatomic) NSArray<past_ingsModel> *past_ings;
#end
#interface resultsModel : JSONModel
#property (strong, nonatomic) ScorecardModel *Scorecard;
#end
#interface queryModel : JSONModel
#property (strong, nonatomic) resultsModel *results;
#end
#interface Past_Match : JSONModel
#property (strong, nonatomic) queryModel *query;
#end
Past_Match.m file Like
#import "Past_Match.h"
#implementation squadModel
#end
#implementation logoModel
#end
#implementation teamsModel
#end
#implementation aModel
#end
#implementation sModel
#end
#implementation tModel
#end
#implementation anewModel
#end
#implementation dModel
#end
#implementation past_ingsModel
#end
#implementation ScorecardModel
#end
#implementation resultsModel
#end
#implementation queryModel
#end
#implementation Past_Match
#end
in the tModel i have issue if i remove // and try to get NSString of c,dt,cd object than my Past_Match object become Null. that all 3 NSString has a same issue i can't get that value. all other value is getting. what i missing something? Thanks in Advance..
This error may occurred due to the optional values in your response object try this.... It may works
#property (strong, nonatomic)NSString<Optional> *c;
#property (strong, nonatomic) NSString<Optional> *dt;
#property (strong, nonatomic) NSString<Optional> *cb;
Tip: Please made all properties to Optional
{ "lastUpdated":0, "firstItemParent":{ "title":"Item 1 title", "text":"Item 1 text", "firstItemList":[ { "title":"Kitchen" }, { "title":"Lounge" }, { "title":"Bathroom" }, { "title":"Garage" } ] } }
need to write RestKit mapping for this data received. Can anybody help me with this.
I tried making firstItemParent element as a class and tried firstItemList as a path nothing worked, and i am new to restkit mapping.
To begin with first you need to create mapping model classes, as per the JSON the mapping classes and properties shown below.
Class ResponseModel
#property (assign,nonatomic) int lastUpdated;
#property (strong, nonatomic) FirstItemParent *firstItemParent;
Class FirstItemParent
#property (strong, nonatomic) NSString *title;
#property (strong, nonatomic) NSString *text;
#property (strong, nonatomic) NSMutableArray *firstItemList;
Class FirstItemList
#property (strong, nonatomic) NSString *title;
After creating the modal classes you have to write the relation mappings to it.
I'm using JSONMODEl (https://github.com/icanzilb/JSONModel) to parse a wordpress JSON FEED (with json-api).
Everything go's well except if I want the "comments".
my feed is like that :
comments = (
{
content = "<p>My comment</p>\n";
date = "2014-08-29 20:56:29";
id = 97813;
name = johndoe;
parent = 0;
url = "http://www.google.com";
}
);
so I try to make my "newsmodel" like that :
#import "JSONModel.h"
#import "commentmodel.h"
#protocol NewsModel #end
#interface NewsModel : JSONModel
#property (strong, nonatomic) NSString* title;
#property (strong, nonatomic) NSString* content;
#property (strong, nonatomic) NSString* thumbnail_images;
#property (strong, nonatomic) NSString* premium;
#property (strong, nonatomic) NSString* id;
#property (strong, nonatomic) CommentModel* comments;
#end
and my commentmodel like that
#import "JSONModel.h"
#interface CommentModel : JSONModel
#property (assign, nonatomic) int id;
#property (strong, nonatomic) NSString* name;
#property (assign, nonatomic) NSString* content;
#end
But When I try to build my app, my "feed" is empty.
if I comment the "comment" part of the news model, I got the content....
I think I'm stuck somewhere, but where ! If someone ave an idea :)
Many thanks
comments is an array, not a single comment, notice the top level ( and ) which designate an array in a NSDictionary NSLog(). Inside of the is an array element designated by { and }.
But the NewsModel has comments defined as a single comment (CommentModel), not an array. it should probably be declared:
In the docs see Model collections and how products is handled.
You will have to declare a protocol, see the example protocol at the top of the "Model collections" examples.
#protocol CommentModel
#end
Above:
#interface CommentModel : JSONModel
#property (strong, nonatomic) NSArray< CommentModel >* comments;
#protocol CommentModel
#end
#interface CommentModel : JSONModel
#property (assign, nonatomic) int id;
#property (strong, nonatomic) NSString* name;
#property (assign, nonatomic) NSString* content;
#end
#interface NewsModel : JSONModel
#property (strong, nonatomic) NSString* title;
#property (strong, nonatomic) NSString* content;
#property (strong, nonatomic) NSString* thumbnail_images;
#property (strong, nonatomic) NSString* premium;
#property (strong, nonatomic) NSString* id; //int?
#property (strong, nonatomic) NSArray<CommentModel>* comments;
#end
Thanks, got it to build, but now If i try to alloc it with
#try {
_feed = [[NewsFeed alloc] initWithDictionary:obj error:nil];
}
#catch (NSException *e) {
NSLog(#"Parse error : %# reason %#", [e name], [e reason]);
}
I got a Bad property protocol declaration reason is not allowed JSONModel property protocol, and not a JSONModel class.
my news feed is like that
#interface NewsFeed : JSONModel
#property (nonatomic, strong) NSArray <NewsModel> *posts;
#end
And work like a charme without the "comment" part...
Thanks
As an addition to the answers above, since I can't add a comment yet, all you have to do, is add an empty protocol with the same name, like this:
#protocol CommentModel
#end
Then, as noted here JsonModel documentation, notation would be different than a notation. The first one is a protocol declaration needed for JsonModel to work, the other one is an objc compiler helper declaration. You can combine them as noted in the same example:
#property (nonatomic) NSArray<ProductModel *> <ProductModel> *products;
I have this Json out of ASP MVC API
I have the InvModel and the LotModel
but when I call
_InvFeed = [[InvModel alloc] initFromURLWithString:#"http://192.168.1.206/service/api/dto/inventory/1?p=Red%20Globe"
completion:^(JSONModel *model, JSONModelError *err)
{
NSLog(#"Inventory: %#", _InvFeed );
NSLog(#"Error: %#",err);
}];
I can not figure out this error:
Error: Error Domain=JSONModelErrorDomain Code=1 "Invalid JSON data:
Attempt to initialize JSONModel object using initWithDictionary:error:
but the dictionary parameter was not an 'NSDictionary'."
UserInfo=0x109075ff0 {NSLocalizedDescription=Invalid JSON data:
Attempt to initialize JSONModel object using initWithDictionary:error:
but the dictionary parameter was not an 'NSDictionary'.,
kJSONModelKeyPath=LotDTO}
and here are the JSONModels for: LotDTO
#import "JSONModel.h"
#interface InvLotModel : JSONModel
#property (assign, nonatomic) int lotid;
#property (strong, nonatomic) NSDate* expdate;
#property (strong, nonatomic) NSString* lotserial;
#property (strong, nonatomic) NSDate* lastupddate;
#property (strong, nonatomic) NSString<Optional>* providerlotserial;
#property (assign, nonatomic) NSDecimal* qtyoriginal;
#property (assign, nonatomic) NSDecimal* qtyallocated;
#property (assign, nonatomic) NSDecimal* qtyavailable;
#property (assign, nonatomic) NSDecimal* qtyonhand;
#property (strong, nonatomic) NSDate* receiptdate;
#property (strong, nonatomic) NSString* linecomment;
#property (assign, nonatomic) NSDecimal* unitcost;
#property (strong, nonatomic) NSString* warehouse;
#end
And here the Inventory Model
#import "JSONModel.h"
#import "InvLotModel.h"
#protocol InvModel #end
#interface InvModel : JSONModel
#property (assign, nonatomic) int id;
#property (strong, nonatomic) NSString* itemid;
#property (strong, nonatomic) NSString* description;
#property (strong, nonatomic) NSDate* createdate;
#property (strong, nonatomic) NSString* createuser;
#property (assign, nonatomic) float lastcost;
#property (assign, nonatomic) BOOL monitorlevel;
#property (assign, nonatomic) int minlevel;
#property (assign, nonatomic) int maxlevel;
#property (strong, nonatomic) NSString* gtin;
#property (assign, nonatomic) float weight;
#property (strong, nonatomic) NSString* uom;
#property (strong, nonatomic) NSString* sizes;
#property (strong, nonatomic) NSString* variety;
#property (strong, nonatomic) NSString <Optional>* bag;
#property (strong, nonatomic) NSString* style;
#property (strong, nonatomic) NSString* box;
#property (strong, nonatomic) NSString* label;
#property (strong, nonatomic) NSString* commodity;
#property (strong, nonatomic) InvLotModel* LotDTO;
#end
I see two problems:
1) In the InvModel class, yo have defined LotDTO as a single object, not an array.
2) In the JSON response you have posted, the syntax for LotDTO does not seem to me valid JSON. It appears to be an array of LotDTO objects, but it does not follow the syntax for JSON arrays (which you can check, for example, here).
Make sure to mark any extension property as Ignore. I faced such an issue trying to copy the model.
Please replace this
#property (strong, nonatomic) InvLotModel* LotDTO;
with this
#property (strong, nonatomic) NSArray<InvLotModel,ConvertOnDemand>* LotDTO;
As officials at JSONModel suggest to use ConvertOnDemand to convert NSArray to JSONModelArray in one of their tutorial to avoid error in implenentation.
This may help you : Click here