iOS - YouTube JSON-C Parsing



我一直试图从YouTube V2 API解析JSON,但它一直在崩溃,我无法找出原因。。。我只是安卓系统的iOS新手。

代码

- (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) 

您的对象videosdatasitems中的一个是Array,而不是Dictionary,因此您不能为它调用objectForKey:

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

因此,将项目强制转换为NSArray:NSArray *itemsArray = (NSArray*)items并通过itemsArray:进行迭代

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

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

最新更新