0

使用 AFNetworking 解析简单的 JSON 是有据可查的,而且看起来非常简单。我想要做的是从 Instagram 解析 JSON,这是一个深入到数组的多个层次,我可以在其中提取图像并插入到 CollectionView 中。现在我被困住了。我的简单代码在单个文本块中返回所有内容,而不是我需要的。我要抓住的东西用红色勾勒出来。如果我抓取 10 张图像,我真的想要将它们全部放在一个数组中。那时我可以抓取实际图像并显示它们。现在我被困住了。

查找所有标记为雪的图像: https ://api.instagram.com/v1/tags/snow/media/recent?access_token=836379.f59def8.fd07b9ba8ea440188dc56d2763bbf6c2

在此处输入图像描述

只返回一串图像名称的代码,这根本不是我想要的。

-(void)viewDidLoad {

  NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

  [super viewDidLoad];

  NSString *path = @"https://api.instagram.com/v1/tags/snow/media/recent?access_token=836379.f59def8.fd07b9ba8ea440188dc56d2763bbf6c2";

  NSLog(@"path %@", path);

  NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:path]];

  AFJSONRequestOperation *operation =
  [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                  success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

                                                    NSString *image = [[[[JSON valueForKey:@"data"]valueForKey:@"images"]valueForKey:@"low_resolution"]valueForKey:@"url"];

                                                    NSLog(@"%@", image);


                                                  } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
                                                    // NSLog(@"There was a problem: %@", [error localizedDescription]);
                                                  }];

  [operation start];

  //NSDictionary *dict = [imagesArry objectAtIndex:0];
}

这就是我得到的:

" http://distilleryimage8.s3.amazonaws.com/3bc4787a401f11e384ec22000ab5caf6_6.jpg ", " http://distilleryimage3.s3.amazonaws.com/d32fb366401d11e3ba2d22000ae90e24_6.jpg ", " http://distilleryimage4.s3.amazonaws.com/903d89f6401911e382a322000a1f9709_6 .jpg ", " http://distilleryimage6.s3.amazonaws.com/84350bf8401d11e393e722000aaa0a4a_6.jpg ", " http://distilleryimage5.s3.amazonaws.com/45387d48401f11e3b9ed22000a1f8cd8_6.jpg ", " http://distilleryimage2.s3 .com/350228c0401f11e3993b22000ae81198_6.jpg ", " http://distilleryimage4.s3.amazonaws.com/6da0b634401e11e3977b22000aeb36ba_6.jpg ", "http://distilleryimage8.s3.amazonaws.com/a0d357722b5c11e3988a22000a1fbac2_6.jpg ", " http://distilleryimage5.s3.amazonaws.com/b2a25ba8400e11e3bfd922000ae90db3_6.jpg ", " http://distilleryimage2.s3.amazonaws.com/1f6c4716401f11e39bdd22000aeb0cb1_6. jpg ", " http://distilleryimage0.s3.amazonaws.com/ca032f722c5711e38c7122000ab5c5fc_6.jpg ", " http://distilleryimage2.s3.amazonaws.com/4ca448be3fde11e3b66b22000aa8003c_6.jpg ", " http://distilleryimage2.s3.amazonaw. com/19fc276c401d11e3b22b22000a1f96e2_6.jpg ", " http://distilleryimage6.s3.amazonaws.com/e36d5a202ddb11e3ac9b22000a1fb864_6.jpg ", "http://distilleryimage3.s3.amazonaws.com/283e1266401f11e3bf992​​2000a1fbc1c_6.jpg ", " http://distilleryimage4.s3.amazonaws.com/01e0ccc6401f11e3854822000ae90109_6.jpg ", " http://distilleryimage10.s3.amazonaws.com/b43573fa401e11e3942822000a1fbb05_6. jpg ", " http://distilleryimage8.s3.amazonaws.com/f6029e5c401e11e3ab8622000ab5c723_6.jpg ", " http://distilleryimage8.s3.amazonaws.com/6d914dde401e11e38c3f22000ae800af_6.jpg ")

好像我的方法不对,就这么简单吗?一行获取我所有的 Instagram 图片链接?嗯,看起来它们确实是用“,”分隔的。会不会这么简单?

NSString *image = [[[[JSON valueForKey:@"data"]valueForKey:@"images"]valueForKey:@"low_resolution"]valueForKey:@"url"];

更新:

好吧,差不多。这有点奇怪,看起来很直接,试图用“,”分割数组,但我确信我正在处理一个字符串:

NSArray *listItems = [图像组件SeparatedByString:@","];

错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSArrayI componentsSeparatedByString:]:无法识别的选择器发送到实例 0x8b84710”

4

1 回答 1

0

谢谢热舔。仅仅因为我说它是 NSTRING 就意味着 zilch。我真的在返回一个数组。

于 2013-11-06T22:08:18.000 回答