我必须遵循来自服务器的 JSON 响应:
{
"theSet": [
],
"Identifikation": 1,
"Name": "Casper",
"Adress": "Lovis 23",
"Location": "At home",
"Information": "The first, the second and the third",
"Thumbnail": "none",
}
我正在检索数据,如下所示:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading");
NSLog(@"Succeeded! Received %d bytes of data",[data length]);
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];
news = [NSArray arrayWithObjects:[res allKeys], [res allValues], nil];
NSLog(@"%@", news);
//[mainTableView reloadData];
}
然后我想将所有 JSON 数据插入到一个数组中,这样我就可以在我的 tableview 中显示数据。
我的表格视图代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
}
cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"Name"];
cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"Adress"];
return cell;
}
但是我的应用程序因错误而崩溃:
-[__NSArrayI objectForKey:]: unrecognized selector sent to instance.
如何将 JSON 对象插入 NSArray,以便在我的 tableview 中显示它?