OGeek|极客世界-中国程序员成长平台

标题: ios - 使用来自 YouTube 用户上传的 JSON 数据填充表格 View - iOS [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:12
标题: ios - 使用来自 YouTube 用户上传的 JSON 数据填充表格 View - iOS

我正在努力使用来自 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4