【问题标题】:Populating Table View with JSON Data from YouTube User's Uploads - iOS使用来自 YouTube 用户上传的 JSON 数据填充表格视图 - iOS
【发布时间】:2014-01-05 07:29:53
【问题描述】:

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

每次我加载 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) 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

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)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"];

【问题讨论】:

    标签: ios json video youtube


    【解决方案1】:

    改变

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多