php - AFNetworking 解析 JSON url 图像错误

标签 php ios json

我正在使用 AFNetworking 将 JSON 文件中的图像、链接和时间解析到我的 iPhone 应用程序中。

JSON 结构如下所示:

[
   {
      "link":"http:\/\/link-to-picture.jpg",
      "title":"Title",
      "published":"19 minutes 'ago' "
   },
   {
      "link":"http:\/\/link-to-picture.jpg",
      "title":"Title",
      "published":"45 minutes 'ago' "
   },
   {
      "link":"http:\/\/link-to-picture.jpg",
      "title":"Title",
      "published":"2 hours 'ago' "
   },
   {
      "link":"http:\/\/link-to-picture.jpg",
      "title":"Title",
      "published":"2 hours 'ago' "
   }

]

我的应用程序代码如下所示:

#import "ViewController.h"

#import "AFNetworking.h"

@implementation ViewController

@synthesize tableView = _tableView, activityIndicatorView = _activityIndicatorView, movies = _movies;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Setting Up Table View
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.tableView.hidden = YES;
    [self.view addSubview:self.tableView];

    // Setting Up Activity Indicator View
    self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityIndicatorView.hidesWhenStopped = YES;
    self.activityIndicatorView.center = self.view.center;
    [self.view addSubview:self.activityIndicatorView];
    [self.activityIndicatorView startAnimating];

    // Initializing Data Source
    self.movies = [[NSArray alloc] init];

    NSURL *url = [[NSURL alloc] initWithString:@"http://link-to-json-file.com/pictureparse.php?name=Name"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        self.movies = [JSON objectForKey:@"results"];
        [self.activityIndicatorView stopAnimating];
        [self.tableView setHidden:NO];
        [self.tableView reloadData];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

// Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.movies && self.movies.count) {
        return self.movies.count;
    } else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"Cell Identifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }

    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
    cell.textLabel.text = [movie objectForKey:@"title"];
    cell.detailTextLabel.text = [movie objectForKey:@"published"];

    NSLog(@"Blabla: %@"), movie;

    NSURL *url = [[NSURL alloc] initWithString:[movie objectForKey:@"link"]];
    [cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];

    return cell;
}

@end

但是当我启动应用程序时,我收到此错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8d40ac0'

我做错了什么?

最佳答案

您的 json 对象似乎是一个包含字典的数组。 NSArray 没有 objectForKey: 方法。您必须从数组中获取您感兴趣的字典对象,然后使用键获取值。

编辑。

如果您的属性 self.movi​​es 具有正确的类型,并且您只想为其分配 json 数组,请尝试 self.movi​​es = JSON;(JSON 对象是一个数组已经有字典了)。

关于php - AFNetworking 解析 JSON url 图像错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19987203/

相关文章:

php - 如何使用 htaccess 从特定 url 中删除正斜杠

php - HTTP 请求失败 - Google map

PHP停止自动添加关闭标签

ios - 高度从 0 变为默认大小后单元格不显示

json - 戈朗 : best way to unmarshal following json with string as keys

php - 如何通过android应用程序在mysql数据库中创建表?

ios - 为什么我从数据库中提取信息的函数不起作用?

ios - MKMapView,如何计算以米为单位的高程?

javascript - 如何使用 Javascript 从 document.viewData 获取 Json 字符串

javascript - 使用 ngInfiniteScroll 从远程源输出 JSON