2013-02-27 80 views
0

我一直在试图解析来自YouTube V2 API的JSON,并且它一直在崩溃,我找不到为什么......我只是从iOS开始接触iOS。iOS - YouTube JSON-C解析

CODE

- (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]; 

} 

堆栈跟踪

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) 
+0

阅读堆栈跟踪,你会发现为什么。 – 2013-02-27 07:55:57

回答

1

您的一个对象videosdatasitems的是一个数组,而不是一个字典,所以你不能叫objectForKey:它。

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

所以,投项目的NSArray:NSArray *itemsArray = (NSArray*)items并反复通itemsArray

for (NSDictionary *item in itemsArray) { 
    NSString *title = [item objectForKey: @"title"]; 
} 

你需要这个,因为可以在项目不止一个视频

+0

我有点困惑,你能告诉我我需要做什么吗? – SquiresSquire 2013-02-27 08:32:54

+0

@SquiresSquire,我已经更新了答案 – 2013-02-27 08:40:03

+0

我加了你说的,但是它对每个单元格都有相同的标题? – SquiresSquire 2013-02-27 09:36:55