从JSON创建数据模型



我有一个看起来像这样的JSON:

{
"club": [
    {
        "titles": "1",
        "league": "epl",
        "country": "england",
    }
}

我创建了这样的属性:

@property (strong, nonatomic) NSMutableArray <Clubs> *club;

俱乐部的财产从具有头衔,联赛和乡村财产的俱乐部阶级继承。

当我尝试使用该数据模型创建字典时,我将无法访问俱乐部数组中的属性。

我是否错误地创建了数据模型?

创建字典

        for (NSDictionary *dictionary in responseObject) {
                if (![self.searchText isEqualToString:@""]) {
                    self.predictiveProductsSearch = [[PerdictiveSearch alloc]initWithDictionary:dictionary error:nil];
                    self.predictiveTableView.dataSource = self;
                    [self.predictiveTableView reloadData];
                    self.predictiveTableView.hidden = NO;
            }
        }

俱乐部班级

 #import <JSONModel/JSONModel.h>

 @protocol Clubs @end
 @interface Clubs : JSONModel
 @property (strong, nonatomic) NSString <Optional> * titles;
 @property (strong, nonatomic) NSString <Optional> * league;
 @property (strong, nonatomic) NSString <Optional> * country;
 @property (strong, nonatomic) NSString <Optional> * topGS;
 @property (strong, nonatomic) NSString <Optional> * GoalSc;
 @property (strong, nonatomic) NSString <Optional> * TransferBudget;

@end

请使用以下代码实现JSON模型保存:

_club = [[NSMutableArray alloc]init];
NSDictionary *responseObject = @{
    @"club": @[
             @{
                 @"titles": @"1",
                 @"league": @"epl",
                 @"country": @"england"
             }]
             };
NSArray *newResponseObject = [responseObject objectForKey:@"club"];
for (NSDictionary *dictionary in newResponseObject) {
    Clubs *objClubs = [[Clubs alloc]initWithDictionary:dictionary error:nil];
    [_club addObject:objClubs];
}
NSLog(@"%@",[_club objectAtIndex:0]);

下面的打印:

<Clubs> 
   [titles]: 1
   [country]: england
   [GoalSc]: <nil>
   [league]: epl
   [topGS]: <nil>
   [TransferBudget]: <nil>
</Clubs>

最新更新