ios - UITableView 未正确提取 JSON

标签 ios xcode json uitableview restkit

编辑- 已解决: 我添加了一个“Feed”模型,它解决了我遇到的映射问题。谢谢!

我想显示 UITableView 行中的所有“标题”,但我遇到了崩溃:

WebListViewController.m-

@interface WebListViewController ()

@property (strong, nonatomic) NSArray *headlinesNow;

@end

@implementation WebListViewController

@synthesize tableView = _tableView;
@synthesize headlinesNow;

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *lAbbreviation = [defaults objectForKey:@"lAbbreviation1"];
    NSString *tID = [defaults objectForKey:@"tId1"];

 NSString *url = [NSString stringWithFormat:@"/now/?l=%@&t=%@&apikey=5xxxxxxxx", lAbbreviation, tID];
    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:url usingBlock:^(RKObjectLoader *loader)  {
        loader.onDidLoadObjects = ^(NSArray *objects) {

headlinesNow = objects;

[_tableView reloadData];

[loader.mappingProvider setMapping:[Headline mapping] forKeyPath:@"feed.headline"];
        loader.onDidLoadResponse = ^(RKResponse *response){
            NSLog(@"BodyAsString: %@", [response bodyAsString]);
        };
    }];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return headlinesNow.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"standardCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    Headline *headlineLocal = [headlinesNow objectAtIndex:indexPath.row];
    NSString *headlineText = [NSString stringWithFormat:@"%@", headlineLocal.headline];
    cell.textLabel.text = headlineText;
    return cell;
}

标题.h

@interface Headline : NSObject
@property (strong, nonatomic) NSString *headline;
@property (strong, nonatomic) Links *linksHeadline;
+ (RKObjectMapping *) mapping;
@end

标题.m

@implementation Headline
+ (RKObjectMapping *)mapping {
    RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) {
        [mapping mapKeyPathsToAttributes:
         @"headline", @"headline",
         nil];
        [mapping hasOne:@"links" withMapping:[Links mapping]];
    }];
    return objectMapping;
}
@end

JSON 响应正文 -

{
    "timestamp": "2013-05-03T22:03:45Z",
    "resultsOffset": 0,
    "status": "success",
    "resultsLimit": 10,
    "breakingNews": [],
    "resultsCount": 341,
    "feed": [{
        "headline": "This is the first headline",
        "lastModified": "2013-05-03T21:33:32Z",
        "premium": false,
        "links": {
            "api": {

我正在获取 bodyAsResponseNSLog 输出,但程序崩溃并且未填充 UITableView。我显然没有正确导航到 JSON?有什么想法吗?

感谢帮助,如果您需要更多代码片段,请告诉我-

编辑- 崩溃是: 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[<__NSCFString 0xe693110> valueForUndefinedKey:]:此类不符合关键标题的键值编码。”

最佳答案

第一个问题是您当前的forKeyPath:@"feed.headline"。它应该只是 forKeyPath:@"feed" 因为该键路径对于您的 JSON 无效。原因是 JSON 中的 feed 是一个数组,因此它应该映射到您的 Headline 对象,然后该对象的映射定义将用于 headline .

下一个问题是 linksHeadline 属性和 [mapping hasOne:@"links"withMapping:[链接映射]];。键名称不匹配。将 Headline 中的属性名称更改为 links

关于ios - UITableView 未正确提取 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16368314/

相关文章:

ios - 我实现了 editActionsForRowAtIndexPath 和 commitEditingStyle 但在滑动单元格时 tableViewCell 上没有出现任何编辑操作

ios - 无法在属性初始值设定项中使用实例成员 'dateType';属性初始值设定项在 'self' 可用之前运行

ios - UIPickerView iOS 11 滚动和选择错误

python - JSON 标准 - 和多态性

ios - 在 removeFromSuperview 之后删除/释放 UIView 的适当方法

ios - Xcode 11 GM Seed 2 在构建工作区/项目时卡住

ios - 如何在 iOS 的 UILabel 上显示 HTML 字符串?

iphone - 所有区域格式列表

python - 将 CSV 转换为 GeoJSON 时如何删除不需要的双引号

r - 如何在R中逐行读取json文件?