首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS - YouTube JSON-C解析

iOS - YouTube JSON-C解析
EN

Stack Overflow用户
提问于 2013-02-27 15:54:21
回答 1查看 650关注 0票数 0

我一直在尝试从YouTube V2应用程序接口解析JSON,但它已经崩溃了,我找不到原因……我只是从安卓系统到iOS的新手。

代码

代码语言:javascript
复制
- (void)viewDidLoad
{
[super viewDidLoad];

self.title = NSLocalizedString(@"Videos", @"Videos");


if ([self.navigationController.parentViewController   respondsToSelector:@selector(revealGesture:)] &&   [self.navigationController.parentViewController respondsToSelector:@selector(revealToggle:)])
{
    UIPanGestureRecognizer *navigationBarPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self.navigationController.parentViewController action:@selector(revealGesture:)];
    [self.navigationController.navigationBar addGestureRecognizer:navigationBarPanGestureRecognizer];

    UIImage *backImage=[UIImage imageNamed:@"NavBarIconLauncher.png"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self.navigationController.parentViewController action:@selector(revealToggle:)];

    //self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(revealToggle:)];

}

[SVProgressHUD showWithStatus:@"Loading Videos..." maskType:SVProgressHUDMaskTypeClear];

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

NSURL *url = [NSURL URLWithString:@"http://gdata.youtube.com/feeds/api/users/YOUTUBE USERNAME/uploads?v=2&alt=jsonc"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self ];
NSLog(@"JSON REQUESTED");

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
[data appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"JSON RECEIVED");
[SVProgressHUD dismiss];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

videos = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
[mainTableView reloadData];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[SVProgressHUD dismiss];
[SVProgressHUD showErrorWithStatus:error.localizedDescription];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[videos valueForKeyPath:@"data.items.title"] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

if(cell == nil){

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
    cell.textLabel.numberOfLines = 1;
    cell.selectionStyle = UITableViewCellAccessoryDisclosureIndicator;
    cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;



}

NSDictionary *datas = [videos objectForKey:@"data"];
NSDictionary *items = [datas objectForKey:@"items"];
NSArray *announcements = [items objectForKey:@"title"];

// NSDictionary* announcement = [announcements objectAtIndex:0];

cell.textLabel.text = [[announcements objectAtIndex:indexPath.row]valueForKey:@"title"];

return cell;
}



- (void)viewDidUnload
{
[super viewDidUnload];

}

堆栈跟踪

代码语言:javascript
复制
2013-03-29 18:35:42.212 SJRC[2818:907] -[__NSCFArray objectForKey:]: unrecognized selector   sent to instance 0x2025d290
2013-03-29 18:35:42.215 SJRC[2818:907] *** Terminating app due to uncaught exception   'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector  sent to instance 0x2025d290'
*** First throw call stack:
(0x326922a3 0x3a30597f 0x32695e07 0x32694531 0x325ebf68 0x107633 0x344e554d 0x344ca313   0x344e17cf 0x3449d803 0x34247d8b 0x34247929 0x3424885d 0x34248243 0x34248051 0x34247eb1  0x326676cd 0x326659c1 0x32665d17 0x325d8ebd 0x325d8d49 0x361892eb 0x344ee301 0xe52d5 0xdd788)
libc++abi.dylib: terminate called throwing an exception
(lldb) 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-27 16:06:16

您的对象videosdatasitems之一是数组,而不是字典,因此您不能为其调用objectForKey:

我已经检查了来自YouTube的响应JSON,可以肯定地说items是一个数组。

因此,将项强制转换为NSArray:NSArray *itemsArray = (NSArray*)items并遍历itemsArray

代码语言:javascript
复制
for (NSDictionary *item in itemsArray) {
   NSString *title = [item objectForKey: @"title"];
}

您需要这样做,因为项目上可能有多个视频

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15106799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档