2013-05-20 35 views
-2

我試圖讓「名字」,「姓氏」,「性別」,從NSDictionary的從NSDictinionary

「homeCity」和「電子郵件」獲得的數據,但它不工作...我嘗試下面的代碼:

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 protected]"; 
      }; 
      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; 
     }; 
    }; 
}; 
    } 
+2

定義「沒有按't work'。編譯錯誤?沒有日誌?房子被燒燬? – Undo

+1

爲什麼你會認爲所有的嵌套對象都會自動找到?你需要逐個下載字典的每一層,首先獲取「userDictionary」然後得到「響應」,然後得到「用戶」等等。瞭解數據結構的人,這個東西並不複雜。 – borrrden

回答

2

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

+0

@AranhaSilva如果我的答案是正確的,你能標記爲已接受嗎?它的工作原理是 – MichaelScaria

+0

thx。而且我確實接受了,因爲這是第一個完整答案 –

1

它不起作用,因爲您的userInfo沒有這樣的密鑰。

userInfo具有userDictionary,其中有metanotificationsresponse

responseuser,並userfirstNamelastName

0

你頂LEVE是用戶信息,但也有 「物」 之內爲好。你將不得不遍歷這些名字。像這樣的東西將起作用:

NSLog(@"first name = %@", userInfo[@"userInfo"][@"userDictionary"][@"response"][@"user"][@"firstName"]); 
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"]);