iosjsonwordpressjsonmodel

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

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


Solution

  • 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;