-2

我正在尝试从 NSDictionary 中获取 "firstName" 、 "lastName" 、 "gender" 、 "homeCity" 和 "email"

但它不起作用......我尝试了以下代码:

NSDictionary *userInfo  = [fsUser getUserInfo:@"self"];

NSLog(@"userInfo: %@", userInfo);
NSLog(@"name: %@", [userInfo objectForKey:@"firstName"]);
NSLog(@"lastName: %@", [userInfo objectForKey:@"lastName"]);
NSLog(@"gender: %@", [userInfo objectForKey:@"gender"]);
NSLog(@"homeCity: %@", [userInfo objectForKey:@"homeCity"]);
NSLog(@"email: %@", [userInfo objectForKey:@"email"]);

这是我的字典值...

  userInfo: {

userDictionary =     {
    meta =         {
        code = 200;
        errorDetail = "Please provide an API version to avoid future errors.See http://bit.ly/vywCav";
        errorType = deprecated;
    };
    notifications =         (
                    {
            item =                 {
                unreadCount = 0;
            };
            type = notificationTray;
        }
    );
    response =         {
        user =             {
            badges =                 {
                count = 0;
                items =                     (
                );
            };
            bio = "";
            checkinPings = off;
            checkins =                 {
                count = 0;
            };
            contact =                 {
                email = "email@email.com";
            };
            firstName = Name;
            following =                 {
                count = 0;
            };
            friends =                 {
                count = 0;
                groups =                     (
                                            {
                        count = 0;
                        items =                             (
                        );
                        name = "Amigos em comum";
                        type = friends;
                    },
                                            {
                        count = 0;
                        items =                             (
                        );
                        name = "Outros amigos";
                        type = others;
                    }
                );
            };
            gender = male;
            homeCity = "";
            id = 3233312;
            lastName = lastName;
            lists =                 {
                groups =                     (
                                            {
                        count = 1;
                        items =                             (
                        );
                        type = created;
                    }
                );
            };
            mayorships =                 {
                count = 0;
                items =                     (
                );
            };
            photo = "https://foursquare.com/img/blank_boy.png";
            photos =                 {
                count = 0;
                items =                     (
                );
            };
            pings = 0;
            referralId = "u-sdsad";
            relationship = self;
            requests =                 {
                count = 0;
            };
            scores =                 {
                checkinsCount = 0;
                goal = 50;
                max = 0;
                recent = 0;
            };
            tips =                 {
                count = 0;
            };
            todos =                 {
                count = 0;
            };
            type = user;
        };
    };
};
   }
4

4 回答 4

2

使用[userInfo objectForKey:@"userDictionary"] objectForKey:@"response"] objectForKey:@"user"] objectForKey:@"homeCity"].

于 2013-05-20T15:34:27.827 回答
2

这就是键值编码 (KVC) 派上用场的地方。你可以这样做(注意使用valueForKey:

NSDictionary *userInfo = [fsUser getUserInfo:@"self"];
NSLog(@"userInfo: %@", userInfo);
NSLog(@"name: %@", [userInfo valueForKey:@"response.user.firstName"]);
NSLog(@"lastName: %@", [userInfo valueForKey:@"response.user.lastName"]);
NSLog(@"gender: %@", [userInfo valueForKey:@"response.user.gender"]);
NSLog(@"homeCity: %@", [userInfo valueForKey:@"response.user.homeCity"]);
NSLog(@"email: %@", [userInfo valueForKey:@"response.user.email"]);

但这不是很有效,因为您一直在深入挖掘同一个字典。这样做会更好:

NSDictionary *userInfo = [fsUser getUserInfo:@"self"];
NSDictionary *user = [userInfo valueForKey:@"response.user"];
NSLog(@"userInfo: %@", userInfo);
NSLog(@"name: %@", [user objectForKey:@"firstName"]);
NSLog(@"lastName: %@", [user objectForKey:@"lastName"]);
NSLog(@"gender: %@", [user objectForKey:@"gender"]);
NSLog(@"homeCity: %@", [user objectForKey:@"homeCity"]);
NSLog(@"email: %@", [user objectForKey:@"email"]);
于 2013-05-20T15:43:54.657 回答
1

它不起作用,因为您userInfo没有这样的密钥。

userInfouserDictionary, 有meta,notificationsresponse.

responseuseruserfirstNamelastName等等。

于 2013-05-20T15:34:58.397 回答
0

您的最高级别是 userInfo,但其中也有“对象”。您将必须遍历这些才能获得名字。像这样的东西会起作用:

NSLog(@"first name = %@", userInfo[@"userInfo"][@"userDictionary"][@"response"][@"user"][@"firstName"]);
于 2013-05-20T15:35:02.490 回答