JSONModel library / model collection wrong

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”.

Read More

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

Related posts

Leave a Reply

4 comments

  1. 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;
    
  2. @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
    
  3. 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

  4. 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;