2013-02-27 177 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