ios - 使用 YouTube 用户上传的 JSON 数据填充 TableView - iOS

标签 ios json video youtube

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

每次我加载 TableView 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) initWithTitle:(NSString *)title;
+ (id) videoWithTitle:(NSString *)title;

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

@end

视频.m

import "Video.h"

@implementation Video


- (id) initWithTitle:(NSString *)title {
    self = [super init];

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

    return self;
}

+ (id) videoWithTitle:(NSString *)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

TableView 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)numberOfSectionsInTableView:(UITableView *)tableView
{

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

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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)prepareForSegue:(UIStoryboardSegue *)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"];

最佳答案

改变

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

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

您的项目数组在第二层:rootJSON -> data -> items

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

相关文章:

ios - 使用什么 API 来检查用户是否要使用漫游电话?

ios - 开始开发 iOS 应用程序 (Swift) : Problems with Implement a Custom Control

video - 添加对自定义视频输入硬件的 QuickTime 支持

video - 如何检查 YouTube 视频是否被阻止/限制/删除

javascript - HTML5 视频标签在 iOS 上不起作用,视频未加载

ios - 从检测到的信标中检索对象

.net - 在构造函数中将 Json 反序列化为 Object

javascript - 如何确保工厂 promise 在指令执行之前解决

ios - Swift - 如何在安装之前在应用程序中保存 json 文件

c++ - OpenCV编码为H264