1

我正在使用 JSONMODEl ( https://github.com/icanzilb/JSONModel ) 来解析 wordpress JSON FEED (with json-api)。

一切顺利,除非我想要“评论”。

我的饲料是这样的:

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";
                }
            );

所以我试着让我的“新闻模特”像这样:

    #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

我的评论模型就是这样

#import "JSONModel.h"
@interface CommentModel : JSONModel

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

但是当我尝试构建我的应用程序时,我的“提要”是空的。

如果我评论新闻模型的“评论”部分,我得到了内容....

我想我被困在某个地方,但是在哪里!如果有人有想法:)

非常感谢

4

4 回答 4

3

comments是一个数组,而不是单个注释,请注意顶层()它在 a 中指定一个数组NSDictionary NSLog()。内部是一个由{和指定的数组元素}

但是NewsModelcomments定义为单个注释 ( CommentModel),而不是数组。它可能应该声明:

在文档中查看模型集合以及如何products处理。

您必须声明一个protocol,请参阅protocol“模型集合”示例顶部的示例。

@protocol CommentModel
@end

以上:

@interface CommentModel : JSONModel
@property (strong, nonatomic) NSArray< CommentModel >* comments;
于 2014-08-29T22:06:02.487 回答
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
于 2014-08-29T22:24:53.867 回答
0

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

于 2014-08-30T08:07:23.033 回答
0

作为上述答案的补充,由于我还不能添加评论,你所要做的就是添加一个同名的空协议,如下所示:

@protocol CommentModel
@end

然后,如此处所述JsonModel 文档,符号将不同于符号。第一个是 JsonModel 工作所需的协议声明,另一个是 objc 编译器助手声明。您可以按照同一示例中的说明组合它们:

@property (nonatomic) NSArray<ProductModel *> <ProductModel> *products;
于 2017-12-20T08:27:10.833 回答