2011-01-06 108 views
0

我做Fconnect在當用戶連接Facebook的我得到這樣 {"id":"100001480456987","name":"Vishnu Gupta","first_name":"Vishnu","last_name":"Gupta","link":"http:\/\/www.facebook.com\/profile.php?id=100001480456987","education":[{"school":{"id":"110885222265513","name":"st.joseph"},"type":"High School"}],"gender":"male","email":"[email protected]","timezone":5.5,"locale":"en_US","verified":true,"updated_time":"2010-11-27T10:10:25+0000"}如何從字符串中檢索子字符串?

字符串現在我想分割用戶的ID名和電子郵件ID,這樣我可以將其存儲在我的數據庫。

誰能告訴我該怎麼辦呢????

回答

3

您不想將一個字符串分解來獲得這些值。相反,你想解析JSON來獲取數據。我已經使用這個庫,它工作得很好:http://stig.github.com/json-framework/

希望這有助於!

編輯:一些示例代碼:

NSDictionary *dict = [responseFromFacebook JSONValue]; 
NSString *facebookID = [dict objectForKey:@"id"]; 
NSString *name = [dict objectForKey:@"name"]; 
NSString *email = [dict objectForKey:@"email"]; 
+0

感謝ü這麼多..........它爲我...... – neha 2011-01-08 04:28:52

0

使用JSON解析器。請參閱this answer以獲取關於可用的不同JSON庫的stackoverflow問題的鏈接。

當然,我也想提一提我自己的JSON解析庫,JSONKit中。在寫這篇文章的時候,我認爲它可以說是最快的JSON解析器。

+0

任何基準數據和代碼可用? – 2011-01-06 08:06:01

0

試試這個

NSDictionary *dict=[[NSDictionary alloc]init]; 
    string=[string stringByReplacingOccurrencesOfString:@"{" withString:@""]; 
    string=[string stringByReplacingOccurrencesOfString:@"}" withString:@""]; 
    string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@""]; 
    NSArray *seperated=[string componentsSeparatedByString:@","]; 
    for(int index=0;index<[seperated count];index++) 
    { 
     NSArray *sub=[[seperated objectAtIndex:index] componentsSeparatedByString:@":"]; 
     [dict setValue:[sub objectAtIndex:0] forKey:[sub objectAtIndex:1]]; 
    } 
相關問題