使用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.amazonaws.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.amazonaws.com/19fc276c401d11e3b22b22000a1f96e2_6.jpg”, “http://distilleryimage6.s3.amazonaws.com/e36d5a202ddb11e3ac9b22000a1fb864_6.jpg”, “http://distilleryimage3.s3.amazonaws.com/283e1266401f11e3bf9922000a1fbc1c_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 = [image componentsSeparatedByString:@“,”];
错误:由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSArrayI componentsSeparatedByString:]:无法识别的选择器发送到实例0x8b84710'
答案 0 :(得分:0)
谢谢Hot Licks。仅仅因为我说这是一个NSTRING意味着zilch。我真的回来了一个数组。