JSONModel库/模型集合错误

问题描述 投票:1回答:4

我正在使用JSONMODEl(https://github.com/icanzilb/JSONModel)来解析wordpress JSON FEED(使用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

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

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

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

非常感谢

ios json wordpress jsonmodel
4个回答
3
投票

comments是一个数组,而不是一个注释,注意顶级()指定NSDictionary NSLog()中的数组。里面是由{}指定的数组元素。

但是NewsModelcomments定义为单个注释(CommentModel),而不是数组。它应该被宣布:

在文档中,请参阅Model collections以及如何处理products

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

@protocol CommentModel
@end

以上:

@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

0
投票

谢谢,让它建立,但现在如果我尝试分配它

@try {
        _feed = [[NewsFeed alloc] initWithDictionary:obj error:nil];

    }
    @catch (NSException *e) {
        NSLog(@"Parse error : %@ reason %@", [e name], [e reason]);
    }

我得到了一个Bad属性协议声明的原因是不允许JSONModel属性协议,而不是JSONModel类。

我的新闻提要就是这样

@interface NewsFeed : JSONModel
@property (nonatomic, strong)   NSArray <NewsModel> *posts;
@end

并且像没有“评论”部分的魅力一样工作......

谢谢


0
投票

作为上述答案的补充,由于我还无法添加评论,所有你需要做的就是添加一个具有相同名称的空协议,如下所示:

@protocol CommentModel
@end

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

@property (nonatomic) NSArray<ProductModel *> <ProductModel> *products;
© www.soinside.com 2019 - 2024. All rights reserved.