首先感谢您的帮助。我还是编程和Objective-C的新手,并且已经按照教程来帮助我完成大部分代码。因此,我尝试从JSON中的cnn.com RSS提要中获取文章并将其显示在表视图中。我使用以下链接:http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=100&q=http://rss.cnn.com/rss/edition.rss使用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;
}
}
答案 0 :(得分:0)
您从JSON中提取了一个NSDictionary。下一步是查看字典中的键。你可以用一行代码来做到这一点
NSLog( @"%@", [dataDictionary allKeys] );
您将看到的一个键是responseData
,从第一个NSLog开始,responseData
似乎是另一个词典。所以你需要提取那个词典
NSDictionary *responseDictionary = dataDictionary[@"responseData"];
然后根据需要重复,直到到达您真正想要的阵列。