planner15
planner15

Reputation: 53

Parsing data from googleapi

I'm trying to build an app that uses data from this url: http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=8&q=http%3A%2F%2Fnews.google.com%2Fnews%3Foutput%3Drss

So far, I'm downloading it with this:

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

and trying to insert it into my table view cells with this:

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

Anyone know what I'm doing wrong?

Upvotes: 0

Views: 68

Answers (2)

navroz
navroz

Reputation: 402

I guess right way for parsing it as follow's

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

I just logged the required keys to console you can add them to arrays and use them anywhere you want.

Upvotes: 0

Nirmal Choudhari
Nirmal Choudhari

Reputation: 559

Your top level object for json is not array so NSDictionary *thread = [self.issueData objectAtIndex:indexPath.row]; this will not work. You top level object is dictionary so parsing will be as

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

Upvotes: 1

Related Questions