从 google api 解析数据

Posted

技术标签:

【中文标题】从 google api 解析数据【英文标题】:Parsing data from googleapi 【发布时间】:2015-02-16 04:42:47 【问题描述】:

我正在尝试构建一个使用来自以下网址的数据的应用程序: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;

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

【问题讨论】:

【参考方案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;

【讨论】:

^谢谢,但有两件事:包含 [entries[indexPath...另外,即使我把那两条线去掉,我也无法让标题出现。 我认为你遗漏了一些东西。我尝试了这段代码并且对我来说工作正常。你能告诉我确切的错误吗?您也可以使用 Google Drive 链接分享屏幕截图【参考方案2】:

我猜正确的方式来解析它如下

- (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];

我刚刚将所需的键记录到控制台,您可以将它们添加到数组并在任何您想要的地方使用它们。

【讨论】:

以上是关于从 google api 解析数据的主要内容,如果未能解决你的问题,请参考以下文章

从Google电子表格中解析格式化文本

使用 Zapier Poll URL 到 Google 表格解析数据时遇到问题

Google Adwords API 响应解析

使用 google+ API 从 google plus 社区和活动获取数据

iOS 从 Google + OAuth 数据中解析用户信息

解析Google自定义搜索API Swift