我试图使用下面的代码来访问字典的内容,并且由于某种原因无法使其工作。如何从JSON数据请求访问NSDictionary的内容
的NSLog(@"self.userCommentsArray %@",self.userCommentsArray);
返回null
感谢任何与此帮助。
NSData *jsonData = [NSData dataWithContentsOfURL:myURL];
NSDictionary *userCommentsDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"userCommentsDictionary %@",userCommentsDictionary); // this works
self.userCommentsArray = [[userCommentsDictionary objectForKey:@"from"] objectForKey:@"name"];
NSLog(@"self.userCommentsArray %@",self.userCommentsArray);
这里的字典中的NSLog输出:
userCommentsDictionary {
data = (
{
created = "2013-07-16T18:42:56+02:00";
from = {
id = 27;
name = "user-4";
};
id = 2553;
message = "liquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
},
{
created = "2013-07-16T18:42:56+02:00";
from = {
id = 28;
name = "user-5";
};
id = 2554;
message = "x ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
},
{
created = "2013-07-19T16:18:56+02:00";
from = {
id = 24;
name = "user-1";
};
id = 5125;
message = test comment;
},
{
created = "2013-07-19T17:00:21+02:00";
from = {
id = 24;
name = "user-1";
};
id = 5126;
message = "test comment ";
}
);
meta = {
totalCount = 18;
};
}
只是看看你的字典。顶级密钥是“数据”,其值是*数组* *。也许你可以从那里弄清楚。 –
是的,一次剥一层洋葱。不要尝试用'[[userCommentsDictionary objectForKey:@“from]] [objectForKey:@”name“]'这样的表达式来”短路“事物,因为它使得不可能调试中间步骤。 (毫无疑问,该表达式返回零)。 –
提示:在userCommentsDictionary中没有关键字“from”。唯一的两个键是“数据”和“元”。 –