2014-08-29 26 views
1

我使用JSONMODEl(https://github.com/icanzilb/JSONModel)來解析wordpress JSON FEED(使用json-api)。JSONModel庫/模型集合錯誤

一切都很好,除非我想要「評論」。

我的飼料就是這樣:

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

,所以我儘量讓我的 「newsmodel」 這樣的:

#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 

和我commentmodel像

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

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

但是當我試圖建立我的應用程序,我的「飼料」是空的。

如果我評論的新聞模式的「註釋」部分,我得到了內容....

我覺得我堅持的地方,但在哪裏!如果有人AVE一個想法:)

非常感謝

回答

3

comments是一個數組,而不是一個單一的評論,發現頂層()其指定的NSDictionaryNSLog()的數組。裏面的是一個由{}指定的數組元素。

NewsModel已將comments定義爲單個註釋(CommentModel),而不是數組。應該聲明:

在文檔中,請參見Model collections以及如何處理products

您將必須聲明,請參閱示例的示例頂部的「模型集合」示例。

@protocol CommentModel 
@end 

上圖:

@interface CommentModel : JSONModel 
@property (strong, nonatomic) NSArray<CommentModel>* comments; 
+0

我試試,但我有錯誤... 而在doc一個「爲‘NewsModel’無法找到協議的聲明」中,有insn't任何協議爲了orderModel。 謝謝 – Mitchum 2014-08-29 22:30:18

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

'id'可能是一個字符串,'NSDictionaly'的NSLog()不包含引號中的所有字符串。 – zaph 2014-08-29 22:28:22

0

謝謝,得到了它的建設,但現在如果我嘗試用

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

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

到的Alloc它,我有一個壞財產協議,聲明的原因不允許使用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;