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

时间:2013-12-17 13:43:22

标签: ios json video youtube

我正在努力使用来自JSON的{​​{1}}数据填充表视图,该数据已被解析(在控制台中记录输出)

每次加载表视图控制器时,都不会填充任何内容。我甚至创建了一个“视频”类(NSObject)。我很难理解我做错了什么。

以下是我的代码: Video.h

Youtube (V 2.1)

Video.m

#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

表视图控制器实现文件(我正在尝试填充的文件)

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

这是JSON which I'm trying to extract from

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

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

解决方案

已更改

#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

为:

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

1 个答案:

答案 0 :(得分:1)

更改

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

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

您的items数组位于第二级: rootJSON - &gt;数据 - &gt;项