• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 使用来自 YouTube 用户上传的 JSON 数据填充表格 View - iOS

[复制链接]
菜鸟教程小白 发表于 2022-12-12 17:12:02 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在努力使用来自 Youtube (V 2.1)JSON 数据填充表格 View ,该数据已被解析(在控制台中记录输出)

每次我加载 Table View Controller 时,都没有填充任何内容。我什至创建了一个“视频”类(NSObject)。我正在努力理解我做错了什么。

以下是我的代码: 视频.h

#import <Foundation/Foundation.h>

@interface Video : NSObject


@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *description;
@property (nonatomic, strong) NSString *thumbnail;
@property (nonatomic, strong) NSString *uploadedDate;
@property (nonatomic, strong) NSURL *url;

// Designated Initializer
- (id) initWithTitleNSString *)title;
+ (id) videoWithTitleNSString *)title;

- (NSURL *) thumbnailURL;
- (NSString *) formattedDate;

@end

视频.m

import "Video.h"

@implementation Video


- (id) initWithTitleNSString *)title {
    self = [super init];

    if ( self ){
        self.title = title;
        self.thumbnail = nil;
    }

    return self;
}

+ (id) videoWithTitleNSString *)title {
    return [[self alloc] initWithTitle:title];
}

- (NSURL *) thumbnailURL {
    //    NSLog(@"%@",[self.thumbnail class]);
    return [NSURL URLWithString:self.thumbnail];
}

- (NSString *) formattedDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat"yyyy-MM-dd HH:mm:ss"];
    NSDate *tempDate = [dateFormatter dateFromString:self.uploadedDate];

    [dateFormatter setDateFormat"EE MMM,dd"];
    return [dateFormatter stringFromDate:tempDate];

}

@end

Table View Controller 实现文件(我要填充的那个)

#import "FilmyViewController.h"
#import "Video.h"

@interface FilmyViewController ()

@end

@implementation FilmyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *videoURL = [NSURL URLWithString"http://gdata.youtube.com/feeds/api/users/OrtoForum/uploads?v=2&alt=jsonc"];

    NSData *jsonData = [NSData dataWithContentsOfURL:videoURL];

    NSError *error = nil;

    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
    NSLog(@"%@",dataDictionary);

    self.videoArray = [NSMutableArray array];

    NSArray *videosArray = [dataDictionary objectForKey"items"];

    for (NSDictionary *vDictionary in videosArray) {
        Video *video = [Video videoWithTitle:[vDictionary objectForKey"title"]];
        video.title = [vDictionary objectForKey"title"];
        video.description = [vDictionary objectForKey"author"];
        video.uploadedDate = [vDictionary objectForKey"uploaded"];
        video.url = [NSURL URLWithString:[vDictionary objectForKey"url"]];
        [self.videoArray addObject:video];
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{

    // Return the number of rows in the section.
    return [self.videoArray count];
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    Video *video = [self.videoArray objectAtIndex:indexPath.row];
    // Configure the cell...
    cell.textLabel.text = video.title;
    cell.textLabel.text = video.description;

    return cell;
}

/*
#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegueUIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

 */

@end

这里是 JSON which I'm trying to extract from .

我寻找了类似的主题,但没有得到任何合适的解决方案。 研究 Link-oneLink-two是我一直试图遵循的。

如果有更好的方法,请告诉我。 我在这里错过了什么?

解决方案

改变了

NSArray *videosArray = [dataDictionary objectForKey"items"];

到:

NSArray *videosArray = dataDictionary[@"data"][@"items"];



Best Answer-推荐答案


改变

NSArray *videosArray = [dataDictionary objectForKey:@"items"];

NSArray *videosArray = dataDictionary[@"data"][@"items"];

您的 items 数组位于第二级:rootJSON -> data -> items

关于ios - 使用来自 YouTube 用户上传的 JSON 数据填充表格 View - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20635801/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap