Gregg Ian
Gregg Ian

Reputation: 58

How to read json fron facebook using jquery

my question maybe very simple for you but for me its very hard because I'm new to jquery and json.

This link https://graph.facebook.com/?ids=http://iphotogra.ph:4732/Images/ViewPhoto?photo_id=3538

will return

{
   "http://iphotogra.ph:4732/Images/ViewPhoto?photo_id=3538": {
      "id": "http://iphotogra.ph:4732/Images/ViewPhoto?photo_id=3538",
      "shares": 6,
      "comments": 2
   }
}

My question is, how can I access the shares and comments using jquery.

I tried this

 var link = "http://iphotogra.ph:4732/Images/ViewPhoto?photo_id=3538";
 jQuery.getJSON('http://graph.facebook.com/?ids=' + link, function (data) {
     console.log(data.shares);
 });

Upvotes: 1

Views: 474

Answers (1)

krishwader
krishwader

Reputation: 11371

You'll have to change console.log(data.shares); to console.log(data[link].shares); because the shares property lies inside an object which is referenced by the link variable.

var link = "http://iphotogra.ph:4732/Images/ViewPhoto?photo_id=3538";
jQuery.getJSON('http://graph.facebook.com/?ids=' + link, function (data) {
   console.log("Shares : " +data[link].shares , "Likes :"+data[link].likes  );
});

Demo: http://jsbin.com/ekuriv/1/edit

Upvotes: 3

Related Questions