2013-08-04 26 views
0

我试图使用下面的代码来访问字典的内容,并且由于某种原因无法使其工作。如何从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; 
     }; 
    } 
+5

只是看看你的字典。顶级密钥是“数据”,其值是*数组* *。也许你可以从那里弄清楚。 –

+0

是的,一次剥一层洋葱。不要尝试用'[[userCommentsDictionary objectForKey:@“from]] [objectForKey:@”name“]'这样的表达式来”短路“事物,因为它使得不可能调试中间步骤。 (毫无疑问,该表达式返回零)。 –

+0

提示:在userCommentsDictionary中没有关键字“from”。唯一的两个键是“数据”和“元”。 –

回答

4

userCommentsDictionary拥有的data的关键一项。该值是一个数组。

你需要的东西是这样的:

NSArray *data = userCommentsDictionary[@"data"]; 
NSDictionary *firstComment = data[0]; 
NSDictionary *from = firstComment[@"from"]; 
NSString *name = from[@"name}; 
self.userCommentsArray = name; 

这里假设你想从第一个注释的数据。根据需要调整。

注意大量中间变量的用户。这使得您的代码更易于阅读和调试。避免这样的行:

self.userCommentsArray = userCommentsDictionary[@"data"][0][@"from"][@"name"]; 

这是很难调试。

更新:如果你真的希望所有名称的数组,那么你可以这样做:

NSArray *data = userCommentsDictionary[@"data"]; 
NSArray *names = [data valueForKeyPath:@"from.name"]; 
self.userCommentsArray = names; 
+0

Typo NAArray :)我学习了新的东西'@“from.name”'很好。感谢&+1。 – TheTiger

+0

错字固定 - 谢谢。 – rmaddy

+2

我认为你需要'valueForKeyPath'在你最后的代码示例中。 –

0

你缺少@"data"关键,这是JSON响应的根源。我假设你想获取所有的名字,所以你可以做这样的事情。

NSArray *data = userCommentsDictionary[@"data"]; 
// data array is an array of dicionaries 

NSArray *from = [data valueForKey:@"from"]; 
//from array will fetch all "from" dictionaries 

NSArray *names = [from valueForKey:@"name"]; 
// names array will contain all names 
-1

我认为你需要从某个索引处的数组中访问一个对象,然后你可以从中选择一个名称字段。例如:

self.userCommentsArray = [userCommentsDictionary objectForKey:@"data"]; 
id someObject = [self.userCommentArray objectAtIndex:0] // or some other index 
NSString *name = [someObject valueForKey:@"name"]; 
+0

这不起作用。对'[self.userCommentArray objectAtIndex:0]'的调用将返回一个'NSDictionary'。字典中没有'name'属性。 – rmaddy

+0

对不起。使用键值:@“name” –

+0

用更正更新您的答案。 – rmaddy