我正在向Facebook提出以下简单请求。我的目标是获取所有用户相册的名称:
[[AppDelegate sharedDelegate].facebook requestWithGraphPath:@"me/albums" andDelegate:[AppDelegate sharedDelegate]];
然后,我从服务器获得JSON响应,如:
data = (
{
"can_upload" = 1;
count = 4;
"cover_photo" = 321491374598618;
"created_time" = "2012-06-04T21:46:23+0000";
from = {
id = 100002132746588;
name = "Owner....";
};
id = 321491371265285;
link = "http://www.facebook.com/album.php?fbid=321491371265285&id=100002132746588&aid=73680";
name = "Photos";
privacy = custom;
type = normal;
"updated_time" = "2012-06-04T22:08:39+0000";
},
{
"can_upload" = 0;
count = 1;
"cover_photo" = 318401854907570;
"created_time" = "2012-05-31T00:00:35+0000";
description = "Which Friend Stalks You the Most?";
from = {
id = 100002132746588;
name = "Owner....";
};
id = 318401848240904;
link = "http://www.facebook.com/album.php?fbid=318401848240904&id=100002132746588&aid=73163";
name = "Which Friend Stalks You the Most?";
privacy = friends;
type = normal;
"updated_time" = "2012-05-31T00:00:36+0000";
},
等等。问题是,当我尝试用以下内容解析数据时:
NSLog(@"%@", [result objectForKey:@"name"]);
我得到null
。我假设发生了这种情况,因为返回数据的NSDictionary
不知道要关注哪个name
条目。如何解析数据以便获取所有相册的名称?
答案 0 :(得分:3)
“result”实际上是一个字典数组,因此循环遍历数组,然后从数组中的每个元素中获取名称。
for (NSDictionary *anAlbum in [result objectForKey:@"data"]) {
NSLog(@"%@", [anAlbum objectForKey:@"name"]);
}