0

I'm trying to access the contents of a dictionary using the following code and can't get it to work for some reason.

the NSLog(@"self.userCommentsArray %@",self.userCommentsArray); returns null

thanks for any help with this.

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); 

here's the nslog output for the dictionary:

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;
        };
    }
4

3 回答 3

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;
于 2013-08-04T18:59:20.013 回答
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
于 2013-08-04T19:04:39.617 回答
-1

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

self.userCommentsArray =  [userCommentsDictionary objectForKey:@"data"];
id someObject  = [self.userCommentArray objectAtIndex:0] // or some other index
NSString *name = [someObject valueForKey:@"name"];
于 2013-08-04T19:10:20.433 回答