从 googleapi 解析数据
Parsing data from googleapi
我正在尝试构建一个使用来自此 url 的数据的应用程序: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];
}
并尝试将其插入到我的 table 视图单元格中:
- (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;
有人知道我做错了什么吗?
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;
}
我想解析它的正确方法如下
- (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];}
我刚刚记录了控制台所需的键,您可以将它们添加到数组并在任何您想要的地方使用它们。
我正在尝试构建一个使用来自此 url 的数据的应用程序: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];
}
并尝试将其插入到我的 table 视图单元格中:
- (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;
有人知道我做错了什么吗?
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;
}
我想解析它的正确方法如下
- (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];}
我刚刚记录了控制台所需的键,您可以将它们添加到数组并在任何您想要的地方使用它们。