surendher
surendher

Reputation: 1378

How to parse the JSON response?

I am working on a project where the facebook's friend list have to de displayed. I did all necessary coding to get the reponse , but the reponse is like the following

 {"data":[{"name":"Ramprasad Santhanam","id":"586416887"},{"name":"Karthik Bhupathy","id":"596843887"},{"name":"Anyembe Chris","id":"647842280"},{"name":"Giri Prasath","id":"647904394"},{"name":"Sadeeshkumar Sengottaiyan","id":"648524395"},{"name":"Thirunavukkarasu Sadaiyappan","id":"648549825"},{"name":"Jeethendra Kumar","id":"650004234"},{"name":"Chandra Sekhar","id":"652259595"}

Can anyone please tell me how to save name and id in two different arrays.

Any help will be appreciated.

Upvotes: 0

Views: 458

Answers (4)

user529758
user529758

Reputation:

This is not HTML. This is JSON. You'll need a JSON parser for this.

A JSON parser would typically make an NSDictionary or NSArray out of the string. With my implementation, you'd do something like this:

NSMutableArray *names = [NSMutableArray array];
NSMutableArray *ids = [NSMutableArray array];

NSDictionary *root = [responseString parseJson];
NSArray *data = [root objectForKey:@"data"];
for (NSDictionary *pair in data)
{
    [names addObject:[pair objectForKey:@"name"]];
    [ids addObject:[pair objectForKey/@"id"]];
}

Recent versions of iOS contain a new Foundation class, NSJSONSerialization, that will handle any JSON parsing and serialization for you.

Upvotes: 1

M.B
M.B

Reputation: 885

you can see below how html response parse . there i am getting facebook friends.

- (void)fbGraphCallback:(id)sender 

    {

    if ( (fbGraph.accessToken == nil) || ([fbGraph.accessToken length] == 0) )


 {

            //restart the authentication process.....
    [fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:) 
                         andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];

} 
else 

{

    NSLog(@"------------>CONGRATULATIONS<------------, You're logged into Facebook...  Your oAuth token is:  %@", fbGraph.accessToken);
    FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:@"me/friends" withGetVars:nil];// me/feed 
    //parse our json
    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *   facebook_response = [parser objectWithString:fb_graph_response.htmlResponse error:nil]; 
    //init array 
    NSMutableArray * feed = (NSMutableArray *) [facebook_response objectForKey:@"data"];
    //  NSMutableArray *recentFriends = [[NSMutableArray alloc] init];
     arr=[[NSMutableArray alloc]init];
    //adding values to array
    for (NSDictionary *d in feed) 
    {
        [arr addObject:d];


    }

    //NSLog(@"array is %@ ",arr);
    [fbSpinner stopAnimating];
    [fbSpinner removeFromSuperview];

    [myTableView reloadData];


}

}

Upvotes: 3

Conrad Shultz
Conrad Shultz

Reputation: 8808

That looks like JSON, not HTML. (You probably already knew this, since you tagged the question with json I see.)

I'm not really sure why others are recommending third-party libraries to do this, unless you need to support rather old OS releases. Just use Apple's built-in NSJSONSerialization class.

Upvotes: 2

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73688

This is json response you are getting. So you need a JSON parser to convert this string into Objective-C objects. In iOS App, you can use a library like the json-framework. This library will allow you to easily parse JSON and generate json from dictionaries / arrays (that's really all JSON is composed of).

From SBJson docs: After JSON parsing you will get this conversion

JSON is mapped to Objective-C types in the following way:

  • null -> NSNull
  • string -> NSString
  • array -> NSMutableArray
  • object -> NSMutableDictionary
  • true -> NSNumber's -numberWithBool:YES
  • false -> NSNumber's -numberWithBool:NO
  • integer up to 19 digits -> NSNumber's -numberWithLongLong:
  • all other numbers -> NSDecimalNumber

Upvotes: 2

Related Questions