AF使用JSON提供objectForKey的网络



我正在尝试访问我的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;
    }
  )
)

因为您使用的是jsonOutputData = [NSMutableArray arrayWithObject:responseObject];。因此,您将responseObject数组放入一个新数组中(因此,您在数组中有一个数组)。然后从数组中获取第一个项,即responseObject(一个数组),并尝试将其作为字典进行访问。

只做

NSLog(@"%@", [[responseObject objectAtIndex:0] objectForKey:@"comments"]);

最新更新