我是 xcode 的新手,我需要帮助。
我有这个json:
{
"noticias": [
{
"titulo":"teste"
}
{
"titulo":"teste 2"
}
]
}
我是怎么得到结果的?我试过
NSDictionary *not = [noticias objectAtIndex:indexPath.row];
但我得到了一个错误,因为我有关键的“noticias”
更新:这是基于迄今为止收到的答案的尝试代码。我有这个:
- (void)fetchNoticias
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
[NSURL URLWithString: @"http://www.site.com/json"]];
NSError* error;
noticias = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
然后
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchNoticias];
}
和我有问题的决赛
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NoticiasCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSArray *noticia = [noticias objectForKey:@"noticias"];
NSDictionary *not = [noticias objectAtIndex:indexPath.row];
NSString *firstTitle = [not objectForKey:@"titulo"];
cell.textLabel.text = firstTitle;
//cell.detailTextLabel.text = subtitulo;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
在线问题:
NSArray *noticia = [noticias objectForKey:@"noticias"];
最佳答案
几点观察:
{
"noticias": [
{
"titulo":"teste"
},
{
"titulo":"teste 2"
}
]
}
如果你不添加缺少的逗号,你会从你的解析器中得到一个错误。例如,
NSJSONSerialization
将报告:Badly formed array around character 109
NSData
使用 JSON 的内容,然后对其进行解析,例如:NSData *data = [NSData dataWithContentsOfFile:filename]; // or [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error)
NSLog(@"%@", error);
else
{
NSArray *array = [dictionary objectForKey:@"noticias"];
NSDictionary *item = [array objectAtIndex:indexPath.row];
NSString *title = [item objectForKey:@"titulo"];
NSLog(@"%@", firstTitle);
}
或者,使用新的、更简单的语法:
NSData *data = [NSData dataWithContentsOfFile:filename]; // or [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error)
NSLog(@"%@", error);
else
{
NSArray *array = dictionary[@"noticias"];
NSDictionary *item = array[indexPath.row];
NSString *title = item[@"titulo"];
NSLog(@"%@", title);
}
首先,为您的
noticias
定义一个属性。在 @interface
为您的类(class):@property (nonatomic, strong) NSArray *noticias;
然后阅读您的
noticias
:- (void)fetchNoticias
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
[NSURL URLWithString: @"http://www.site.com/json"]];
NSError* error = nil;
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
if (error)
{
NSLog(@"%s: error=%@", __FUNCTION__, error);
return;
}
self.noticias = [results objectForKey:@"noticias"];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
然后
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchNoticias];
}
最后,
cellForRowAtIndexPath
:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NoticiasCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *noticia = [self.noticias objectAtIndex:indexPath.row];
NSString *firstTitle = [noticia objectForKey:@"titulo"];
cell.textLabel.text = firstTitle;
//cell.detailTextLabel.text = subtitulo;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
关于ios - xcode NSDictionary 错误多数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16826988/