我正在尝试访问我的JSON,我完成了那部分。但是当我想访问JSON中的特定项目时,我得到了
'-[__NSCFArray objectForKey:]: unrecognized selector sent to instance
以下是代码:
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCredential:credentials];
[operation setResponseSerializer:[AFJSONResponseSerializer serializer]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success");
jsonOutputData = [NSMutableArray arrayWithObject:responseObject];
NSLog(@"%@", [[jsonOutputData objectAtIndex:0] objectForKey:@"comments"]);
NSLog(@"The Array: %@",jsonOutputData);
//[self.newsFeed reloadData];
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"Failure: %@", error);
}];
[operation start];
我的JSON非常简单:
[
{
"id":"2043",
"user_id":"5",
"created_time":"2015-03-24 22:32:58",
"post_type":"3",
"post_link":"google.ca",
"message":null,
"photo":{
"height":"266",
"width":"720",
"link":"google.ca",
"thumbnail":"jpg",
"source":"jpg"
},
"likes":"88029",
"shares":"170",
"comments":"850",
}
]
任何形式的帮助将不胜感激!如果有任何问题,请询问,我会尽力为您提供必要的信息!
由于
这是JSON输出
(
(
{
comments = 868;
"created_time" = "2015-03-24 22:32:58";
id = 2043;
likes = 88309;
message = "<null>";
photo = {
height = 266;
link = "google.ca";
thumbnail = "jpg";
width = 720;
};
"post_link" = "google.ca";
"post_type" = 3;
shares = 175;
"user_id" = 5;
}
)
)
答案 0 :(得分:1)
因为您正在使用jsonOutputData = [NSMutableArray arrayWithObject:responseObject];
。因此,您将responseObject
数组放入一个新数组中(因此您在数组中有一个数组)。然后你得到数组中的第一个项目,即responseObject
- 一个数组 - 并尝试将其作为字典访问。
只做
NSLog(@"%@", [[responseObject objectAtIndex:0] objectForKey:@"comments"]);