如何从字符串中检索子字符串?

时间:2011-01-06 07:22:56

标签: iphone

我正在做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":"vishu.gupta20@gmail.com","timezone":5.5,"locale":"en_US","verified":true,"updated_time":"2010-11-27T10:10:25+0000"}

现在我要拆分用户的id名称和电子邮件ID,以便将其存储在我的数据库中。

有人可以告诉我该怎么做吗????

4 个答案:

答案 0 :(得分: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"];

答案 1 :(得分:0)

这看起来像JSON。有关使用Objective C进行JSON处理的一些信息,请访问: http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c

答案 2 :(得分:0)

使用JSON解析器。有关可用的不同JSON库的stackoverflow问题的链接,请参阅this answer

当然,我还想提一下我自己的JSON解析库JSONKit。在撰写本文时,我认为可以说它是最快的JSON解析器。

答案 3 :(得分: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]];
    }