gpt4 book ai didi

ios - 解析来自 googleapi 的数据

转载 作者:行者123 更新时间:2023-11-29 02:17:55 25 4
gpt4 key购买 nike

我正在尝试构建一个使用来自以下网址的数据的应用程序:http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=8&q=http%3A%2F%2Fnews.google.com%2Fnews%3Foutput%3Drss

到目前为止,我正在用这个下载它:

- (void) downloadData {
NSString *url = @"http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=8&q=http%3A%2F%2Fnews.google.com%2Fnews%3Foutput%3Drss";

// Create NSUrlSession
NSURLSession *session = [NSURLSession sharedSession];

// Create data download taks
[[session dataTaskWithURL:[NSURL URLWithString:url]
completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {

NSError *jsonError;
self.issueData = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&jsonError];
// Log the data for debugging
NSLog(@"DownloadedData:%@",self.issueData);

// Use dispatch_async to update the table on the main thread
// Remember that NSURLSession is downloading in the background
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}] resume];
}

并尝试将其插入到我的表格 View 单元格中:

- (CustomTableViewCell *)tableView:(UITableView *)tableView     cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customTableViewCell" forIndexPath:indexPath];

NSLog(@"Working on cell:%ld",(long)indexPath.row);
NSDictionary *thread = [self.issueData objectAtIndex:indexPath.row];
cell.title.text = [thread objectForKey:@"description"];

cell.date.text = [thread objectForKey:@"publishedDate"];

cell.content.text = [thread objectForKey:@"contentSnippet"];


return cell;

有人知道我做错了什么吗?

最佳答案

您的 json 顶级对象不是数组,所以 NSDictionary *thread = [self.issueData objectAtIndex:indexPath.row];这是行不通的。您的顶级对象是字典,因此解析将如下

(CustomTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customTableViewCell" forIndexPath:indexPath];

NSLog(@"Working on cell:%ld",(long)indexPath.row);
NSDictionary *thread = [self.issueData objectForKey:@"responseData"];
NSDictionary *feed = [thread objectForKey:@"feed"];

cell.title.text = [feed objectForKey:@"description"];

NSArray *entries = [feed objectForKey:@"entries"];
NSDictionary *posts = entries[indexPath.row];
cell.date.text = [posts objectForKey:@"publishedDate"];

cell.content.text = [posts objectForKey:@"contentSnippet"];


return cell;
}

关于ios - 解析来自 googleapi 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28534648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com