user1872384
user1872384

Reputation: 7127

How to parse facebook likes json response?

I managed to get the user likes using this code:

[FBRequestConnection startWithGraphPath:@"/me/likes"
                             parameters:nil
                             HTTPMethod:@"GET"
                      completionHandler:^(
                                          FBRequestConnection *connection,
                                          id result,
                                          NSError *error
                                          ) {
                          /* handle the result */
                          NSLog(@"User interests data: %@",result);
                          self.likesTextView.text = [NSString stringWithFormat:@"User interests data: %@",result];

}];

This is the log:

User interests data: {

data =     (

            {

        category = Company;

        "created_time" = "2015-04-09T11:31:13+0000";

        id = 403422586500762;

        name = "Sky3888 iOS & Android - Officially Agent";

    },

            {

        category = "Games/toys";

        "created_time" = "2015-04-09T11:30:57+0000";

        id = 1575053229378162;

        name = i12win;

    },

            {

        category = "Shopping/retail";

        "created_time" = "2015-04-07T04:44:09+0000";

        id = 273182176129865;

        name = Tinkerbelle;

    }
 );

However how do I get and display the "name" and "category" field in a UILabel?

Upvotes: 0

Views: 201

Answers (2)

Vidhyanand
Vidhyanand

Reputation: 5369

You can do as below

NSDictionary *responseDict=(NSDictionary *)result;
    NSArray *mainArray=[responseDict objectForKey:@"data"];

    for (NSDictionary *dict in mainArray) {
        nameLabel.text=[dict valueForKey:@"name"];
        catLabel.text=[dict valueForKey:@"category"];
    }

Hope it helps you..!

Upvotes: 1

Ruud Kalis
Ruud Kalis

Reputation: 264

Make an NSArray of the result and then loop through the objects in it performing the function objectForKeyon category and name to extract the data you want.

There is a similar question answered here: NSMutableArray losing all objects

You should be able to use the code from that and copy the results into your UILabel.

Upvotes: 0

Related Questions