使用GooglePlus类从NSDictionary获取键值

时间:2013-10-08 16:37:21

标签: ios objective-c nsstring nsdictionary google-plus

我有一个名为itemDict的NSDictionary和打印时

NSLog(@"itemDictValues:%@",itemDict);

输出采用以下格式:

itemDictValues:
GTLPlusPerson 0xab821e0: 
{etag:""LTv_6IJISeUQGTVXLjMeOtebkoM/eup2crXcelmpMFKesXWlGkJjCiE"" kind:"plus#person" id:"1145282979128841" objectType:"person" displayName:"FirstName LastName" image:{url} url:"https://plus.google.com/1145282979128841"}

由此我需要将 id displayName url 对应的值提取到我的NSString变量中,格式为

profileId=1145282979128841;

Name=FirstName LastName;

Profilepic=https://plus.google.com/1145282979128841;

我该怎么做?

5 个答案:

答案 0 :(得分:4)

试试这个......

_profileId=(NSString*)((GTLPlusPerson*)itemDict).identifier;
_profileName= (NSString*)((GTLPlusPerson*)itemDict).displayName;
_profileImageURLPath=(NSString*)((GTLPlusPerson*)itemDict).image.url;

答案 1 :(得分:0)

对于NSDictionary

     NSString *profileId = itemDict[@"id"]
     NSString *name = itemDict[@"displayName"]
     NSString *profilePic = itemDict[@"image"][@"url"]

如果是GTLPlusPerson对象,则使用objective-c dot syntax

访问属性
     NSString *profileId = itemDict.id;
     NSString *name = itemDict.displayName;
     NSString *profilePic = itemDict.url;

根据您的日志判断,它似乎是一个GTLPlusPerson对象而不是NSDictionary

答案 2 :(得分:0)

您只需解析词典即可获得所需信息。

NSString *profileId = [itemDict objectForKey:@"id"];
NSString *displayName = [itemDict objectForKey:@"displayName"];

现在那个图像,我认为这本身就是字典。既然你没有指明字典里面的内容,那么我认为你应该这样做:

NSDictionary *imageDict = [itemDict objectForKey:@"image"];
NSURL *imageURL = [imageDict objectForKey:@"url"];

然后获得UIImage变得非常简单:

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:imageURL];
NSData *imageData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
UIImage *image = [[UIImage alloc] initWithData:imageData];

您应该选择sendAsynchronousRequest方法,因为它在后台发生,并且不会阻碍您应用的功能。

答案 3 :(得分:0)

NSArray* peopleList = peopleFeed.items;
NSLog(@"peopleList %@ ",peopleList.description);
for (NSArray *dict in peopleFeed.items) {
                    NSString *peopleStrID=(NSString*)((GTLPlusPerson*)dict).identifier;
                    NSLog(@"peopleStrID %@",peopleStrID);
                    NSString *peopleName = (NSString*)((GTLPlusPerson*)dict).displayName;
                    NSString *peoplePic = (NSString*)((GTLPlusPerson*)dict).image.url;
                }

答案 4 :(得分:0)

最简单的方法是使用Google提供的代码:

    if ([[GPPSignIn sharedInstance] authentication]) {
        // The user is signed in.

        GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"];


        GTLServicePlus* plusService = [[GTLServicePlus alloc] init];
            plusService.retryEnabled = YES;

        //auth = GTMOAuth2Authentication object from login          

        [plusService setAuthorizer:auth];

        [plusService executeQuery:query
                completionHandler:^(GTLServiceTicket *ticket,
                                    GTLPlusPerson *person,
                                    NSError *error) {
                    if (error) {
                        GTMLoggerError(@"Error: %@", error);
                    } else {
                        // Retrieve the display name and "about me" text
                        NSString *description = [NSString stringWithFormat:@"%@\n%@", person.displayName, person.aboutMe];
                        NSString *imageURL = person.image.url;

                    }
                }];



    } else {
        //user is logged out
    }
}