ios - 显示 JSON 响应中的 cell.textLabel.text

标签 ios objective-c json uitableview

我需要将 "href" 网址显示为 UITableView 中的 cell.textlabel.text。任何帮助将不胜感激。

JSON:

"grandfathers": [{
        "id": "60",
        "sons": [{
            "id": "30",
            "grandsons": [{
                "id": "10",
                "links": {
                    "api": {
                        "news": {
                            "href": "http://api.website.com/v1/family/grandfathers/sons/grandsons/10/news"
                        },
                    },

@interface:

@property (strong, nonatomic) NSArray *grandfathers;
@property (strong, nonatomic) NSMutableArray *sons;
@property (strong, nonatomic) NSArray *grandsons;

表格 View :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"standardCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    Grandfather *grandfatherLocal = [grandfathers objectAtIndex:0];
    Son *sonLocal = [grandfatherLocal.sons objectAtIndex:0];

    // Not sure what to do here to get to the "href" eventually
    // This was my best guess...
    Grandson *grandsonLocal = [sonLocal.grandsons objectAtIndex:indexPath.row];
    cell.textLabel.text = [grandsonLocal.links.api.news.href valueForKey:@"href"];
};

我不确定此时如何获取嵌套数据。 (仅供引用,我设置了模型并使用 RestKit,这就是我在这里所做的上下文。)

编辑: 我在表中没有显示任何内容,输出中的错误只是:“无法找到 keyPath 的对象映射:''”

编辑: 根据请求的孙子接口(interface):

@interface Team : NSObject
@property (nonatomic, strong) NSNumber *id;
@property (nonatomic, strong) Links *links;
+ (RKObjectMapping *) mapping;
@end

编辑: 亚历山德罗完美地回答了我的问题,见下文。顺便说一句,他不遗余力地帮助我,我非常感激-

谢谢!

最佳答案

如果正确获取了grandsonLocal,那么我认为您应该使用

    cell.textLabel.text = grandsonLocal.links.api.news.href;

如果这不起作用,请尝试在该行中放置一个断点并检查 grandsonLocal 及其属性的内容。

关于ios - 显示 JSON 响应中的 cell.textLabel.text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16325840/

相关文章:

ios - 有什么最简单的方法可以在 iOS 中实现所有必需的协议(protocol)方法吗?

ios - NSDictionary writetofile无法正常工作

iOS:在调用 reloadRowsAtIndexPaths 时,didEndDisplayingCell 被调用两次

ios - 从 OpenGL 绘图创建 png UIImage

ios - 从一个 View 到另一 View 更新标签内容

ios - (iOS) 加速度计图(将重力转换为 +/- 128)粒度

ios - Swift 2 中的 "Extra argument ' 错误 ' in call"错误

java - C# 中反序列化 json java 对象

php - 是否可以使用 PHP 从 API 调用中仅接收 JSON 格式的部分结果?

javascript - 如何从 json-server 将对象数组存储在 db.json 中