Ios UITableView和JSON错误

Ios UITableView和JSON错误,ios,objective-c,json,uitableview,Ios,Objective C,Json,Uitableview,我试图通过url用JSON的结果填充UITableView。我得到一个神秘的错误(对我来说很神秘,因为这是我的第一个iOS应用程序) 这是我的密码: #import "VideoListViewController.h" #import "Videos.h" #import "JSONLoader.h" @implementation VideoListViewController{ NSArray *_videos; } - (void)viewDidLoad { [super view

我试图通过url用JSON的结果填充UITableView。我得到一个神秘的错误(对我来说很神秘,因为这是我的第一个iOS应用程序)

这是我的密码:

#import "VideoListViewController.h"
#import "Videos.h"
#import "JSONLoader.h"


@implementation VideoListViewController{
NSArray *_videos;
}

- (void)viewDidLoad {
[super viewDidLoad];

// Create a new JSONLoader with a local file URL
JSONLoader *jsonLoader = [[JSONLoader alloc] init];
NSURL *url = [NSURL URLWithString:@"https://api.wistia.com/v1/medias.json?api_password=1b75e458de33a9b3f99d33f6bf409a7e145c570a&project_id=kx3rkgrv2w"];

// Load the data on a background queue...
// As we are using a local file it's not really necessary, but if we were connecting to an online URL then we'd need it
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    _videos = [jsonLoader locationsFromJSONFile:url];
    // Now that we have the data, reload the table data on the main UI thread
    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
});
 }



#pragma mark - Table View Controller Methods

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

Videos *videos = [_videos objectAtIndex:indexPath.row];

cell.textLabel.text = videos.name;
cell.detailTextLabel.text = videos.id;
cell.imageView.image = [UIImage imageNamed:@"chat_video.png"];

return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_videos count];
}



@end
它永远不会通过dispatch_async调用,它只是跳过它,然后出错,出现以下错误:

“NSInvalidArgumentException”,原因:“-[\uu NSCFArray objectForKey:]:无法识别的选择器已发送到实例0x8c9d0d0”

我注意到返回的json格式没有为我提取的信息选择符,这可能就是问题所在

谢谢, 萨姆

编辑 以下是JSONLoader方法:

- (NSArray *)locationsFromJSONFile:(NSURL *)url {
// Create a NSURLRequest with the given URL
NSURLRequest *request = [NSURLRequest requestWithURL:url
                                         cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                     timeoutInterval:30.0];

// Get the data
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

// Now create a NSDictionary from the JSON data
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

// Create a new array to hold the locations
NSMutableArray *videos = [[NSMutableArray alloc] init];

// Get an array of dictionaries with the key "locations"
NSArray *array = [jsonDictionary objectForKey:@""];
// Iterate through the array of dictionaries
for(NSDictionary *dict in array) {
    // Create a new Location object for each one and initialise it with information in the dictionary
    Videos *video = [[Videos alloc] initWithJSONDictionary:dict];
    // Add the Location object to the array
    [videos addObject:video];
}

// Return the array of Location objects
return videos;
}

大多数(如果不是全部的话)Objective-c JSON解析器将对象转换为NSDictionary类的实例,将数组转换为NSArray类的实例


程序中的某个地方发生的情况是,它假设它处理NSDictionary,实际上它有一个NSArray。我不知道你的JSON看起来是什么样子,所以不能说它发生在哪里。使用断点来查找。问题可能出在JSONLoader类中,因此如果您自己制作,最好在那里检查。如果您仍然有问题,请向我们展示locationsFromJSONFile方法的代码。

看起来您的问题与访问json.org学习json格式的人的问题完全相同。如果你阅读速度慢,需要5分钟,10分钟。然后在解析之前,使用NSLog转储接收到的JSON。(如有必要,首先使用NSString initWithData将收到的NSData转换为NSString。)最外层的JSON结构是“对象”还是“数组”?(或者如果您想用NSLog转储解析的JSON,您可以这样做,只需了解NSArray不是围绕JSON数组的
[]
,而是有
()
)而且,顺便说一句,了解如何读取异常回溯或使用异常断点来标识异常的位置。谢谢,我想我在iOS上的第一次尝试会因为C#::而不顺利。我编辑了我的帖子并添加了JSONLoader。最外层的JSON似乎是一个对象,这就是我从中获取两个字段的原因。它在这里失败://获取一个字典数组,其键为“locations”NSArray*array=[jsonDictionary objectForKey:@”“;这就是我删除objectForKey的地方,因为最外层的JSON没有密钥,它只是一个具有如下字段的对象“[{”id“:2687053,“name:“Video One-Build”,“type”,“Video”,“created:“2013-01-30T19:29:49+00:00”,“updated:“2013-03-01T21:04:53+00:00”,“duration:”151.48,“hashed_id:“23v34h4ug4”,“description:”,“progress:“1.0”,“status:“ready:”,“缩略图”:{“url”:“}”因此,如果JSON没有objectForKey,我应该将这行设置为什么?NSArray*数组=[jsonDictionary objectForKey:@“results”];从JSON中,[NSJSONSerialization JSONObjectWithData:data选项:0错误:nil]方法将返回NSArray的实例,而不是NSDictionary。它应该是NSArray*数组=[NSJSONSerialization JSONObjectWithData:data options:0错误:nil]我尝试了这一点,现在我得到了错误:“'NSInvalidArgumentException',原因:'-[\uu NSCFNumber IsequalString:]:发送到实例0x8c460b0的无法识别的选择器”