0

I am doing Fconnect in that when a user connects to facebook I get a string like this {"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"}

Now I want to split the id name and email id of the user so that I can store it in my database.

Can someone tell me how to do it????

RAS
  • 8,100
  • 16
  • 64
  • 86
neha
  • 361
  • 6
  • 12

4 Answers4

3

You don't want to split a string to get those values. Instead, you want to parse the JSON to grab data. I've used this library, it works very well: http://stig.github.com/json-framework/

Hope this helps!

EDIT: Some sample code:

NSDictionary *dict = [responseFromFacebook JSONValue];
NSString *facebookID = [dict objectForKey:@"id"];
NSString *name = [dict objectForKey:@"name"];
NSString *email = [dict objectForKey:@"email"];
donkim
  • 13,119
  • 3
  • 42
  • 47
0

this looks like JSON. Some information on JSON handling with Objective C is available at http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c

0

Use a JSON parser. See this answer for links to stackoverflow questions about the different JSON libraries available.

Of course I'd also like to mention my own JSON parsing library, JSONKit. At the time of this writing I think it's fair to say that it's the fastest JSON parser out there.

Community
  • 1
  • 1
johne
  • 6,760
  • 2
  • 24
  • 25
  • [json-benchmarks](https://github.com/samsoffes/json-benchmarks), [What's the most efficient way of converting a 10 MB JSON response into an NSDictionary?](http://stackoverflow.com/questions/4351930/whats-the-most-efficient-way-of-converting-a-10-mb-json-response-into-an-nsdicti), [Cocoa JSON parsing libraries, part 2](http://psionides.jogger.pl/2010/12/12/cocoa-json-parsing-libraries-part-2/) – johne Jan 06 '11 at 12:02
0

Try this

    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]];
    }
KingofBliss
  • 15,055
  • 6
  • 50
  • 72