我正在尝试构建一个使用此网址数据的应用: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];
}
并尝试将其插入我的表格视图单元格中:
- (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;
任何人都知道我做错了什么?
答案 0 :(得分:1)
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;
}
答案 1 :(得分:0)
我想正确的解析方法如下
- (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;
NSDictionary * dict;
dict= [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&jsonError];
dispatch_async(dispatch_get_main_queue(), ^{
if ([dict valueForKey:@"responseData"]) {
if([[dict valueForKey:@"responseData"] isKindOfClass:[NSDictionary class]]){
if ([(NSDictionary *)[dict valueForKey:@"responseData"] valueForKey:@"feed"]) {
if ([[(NSDictionary *)[dict valueForKey:@"responseData"] valueForKey:@"feed"]isKindOfClass:[NSDictionary class]]) {
NSDictionary * feedDict = [(NSDictionary *)[dict valueForKey:@"responseData"] valueForKey:@"feed"];
if ([feedDict valueForKey:@"description"]){
NSLog(@"%@",[feedDict valueForKey:@"description"]);
}
if ([[feedDict valueForKey:@"entries"] isKindOfClass:[NSArray class]]){
for (NSDictionary * dic in (NSArray *)[feedDict valueForKey:@"entries"]) {
NSLog(@"published = %@",[dic valueForKey:@"publishedDate"]);
}
}
if ([[feedDict valueForKey:@"entries"] isKindOfClass:[NSArray class]] ){
for (NSDictionary * dic in (NSArray *)[feedDict valueForKey:@"entries"]) {
NSLog(@"contentSnippet = %@",[dic valueForKey:@"contentSnippet"]);
}
}
}
}
}
}
});
}] resume];}
我刚刚将所需的密钥记录到控制台,您可以将它们添加到阵列并在任何地方使用它们。