【问题标题】:how to retrieve a substring from a string?如何从字符串中检索子字符串?
【发布时间】:2011-01-06 07:22:55
【问题描述】:

我正在做 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,以便我可以将其存储在我的数据库中。

谁能告诉我怎么做????

【问题讨论】:

    标签: iphone


    【解决方案1】:

    您不想拆分字符串来获取这些值。相反,您想要解析 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"];
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        【解决方案4】:

        试试这个

            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]];
            }
        

        【讨论】:

          猜你喜欢
          • 2012-02-13
          • 1970-01-01
          • 2018-05-22
          • 2017-01-04
          • 1970-01-01
          • 2013-01-20
          • 1970-01-01
          • 1970-01-01
          • 2013-03-26
          相关资源
          最近更新 更多