Objective c 解析的JSON数据未显示在表视图中

Objective c 解析的JSON数据未显示在表视图中,objective-c,json,rss,Objective C,Json,Rss,首先谢谢你的帮助。我对编程和objective-C仍然是新手,并且已经跟随一个教程来帮助我完成大部分代码。因此,我试图从cnn.com RSS提要中获取JSON格式的文章,并将其显示在表视图中。我使用以下链接:使用chrome中的JSONView扩展将xml转换为JSON 以下是chrome的输出: { responseData: { feed: { feedUrl: "http://rss.cnn.com/rss/edition.rss", title: "CNN.com - Top Stor

首先谢谢你的帮助。我对编程和objective-C仍然是新手,并且已经跟随一个教程来帮助我完成大部分代码。因此,我试图从cnn.com RSS提要中获取JSON格式的文章,并将其显示在表视图中。我使用以下链接:使用chrome中的JSONView扩展将xml转换为JSON

以下是chrome的输出:

{
responseData: {
feed: {
feedUrl: "http://rss.cnn.com/rss/edition.rss",
title: "CNN.com - Top Stories",
link: "http://edition.cnn.com/index.html?eref=edition",
author: "",
description: "CNN.com delivers up-to-the-minute news and information on the latest top           
stories, weather, entertainment, politics and more.",
type: "rss20",
entries: [
{
title: "How to overcome shyness",
link: "http://edition.cnn.com/2014/06/03/living/how-to-overcome-shyness-relate-real-   
simple/index.html",
author: "",
publishedDate: "Tue, 03 Jun 2014 11:49:55 -0700",
contentSnippet: "Skills and lessons from improv comedy can help you beat shyness.",
content: "Skills and lessons from improv comedy can help you beat shyness.",
categories: [ ]
},
{
title: "Battle for Ukraine border base",
link: "http://edition.cnn.com/2014/06/02/world/europe/ukraine-crisis/index.html",
author: "",
publishedDate: "Mon, 02 Jun 2014 15:44:24 -0700",
contentSnippet: "Five militants are killed after at least 100 tried to storm a border     
guard base in the eastern city of Luhansk.",
content: "Five militants are killed after at least 100 tried to storm a border guard base    
in the eastern city of Luhansk.",
categories: [ ]
},
responseDetails: null,
responseStatus: 200
}
我不相信我使用了正确的键和值对,因为当我运行模拟器时,我得到了一个空的表视图。dataDictionary的NSLog显示数据已经被解析,但我知道我做错了什么。这是我的密码:

#import "TableViewController.h"
#import "BlogPost.h"
#import "WebViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *blogURL = [NSURL    
    URLWithString:@"http://ajax.googleapis.com/ajax/services/feed/load?   
    v=1.0&num=100&q=http://www.huffingtonpost.com/feeds/index.xml"];

    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

    NSError *error = nil;

    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData 
      options:0 error:&error];
    NSLog(@"%@",dataDictionary);

    self.blogPosts = [NSMutableArray array];

    NSArray *blogPostsArray = [dataDictionary objectForKey:@"entries"];

    for (NSDictionary *bpDictionary in blogPostsArray) {
        BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary 
           objectForKey:@"title"]];
        blogPost.date = [bpDictionary objectForKey:@"publishedDate"];
        blogPost.url = [NSURL URLWithString:[bpDictionary objectForKey:@"feedUrl"]];
        [self.blogPosts addObject:blogPost];
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.blogPosts count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath 
      *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
      forIndexPath:indexPath];

    BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];

    cell.textLabel.text = blogPost.title;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",
    [blogPost formattedDate]];
    return cell;
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"preparing for segue: %@",segue.identifier);

    if ( [segue.identifier isEqualToString:@"showBlogPost"]){
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];
        WebViewController *wvc = (WebViewController *)segue.destinationViewController;
        wvc.blogPostURL = blogPost.url;

    }
}

您已经从JSON中提取了一个NSDictionary。下一步是查看字典中的键。你可以用这样一行代码来实现

NSLog( @"%@", [dataDictionary allKeys] );
您将看到的一个键是
responseData
,从第一个NSLog来看,
responseData
似乎是另一个字典。所以你需要提取那本字典

NSDictionary *responseDictionary = dataDictionary[@"responseData"];
然后根据需要重复,直到到达实际需要的阵列