Search code examples
javascriptfacebooksdkgetjson

Javascript get graph facebook array query


Hello dear everyone here,

Today i stack on Javascript FB Graph, I try to get the counting share, like from url which shared to facebook using Ajax as my code below. Is it possible to do so ? or is there other method ? Thank you.

  var api_fb_ul = "https://graph.facebook.com/fql?q=SELECT url, normalized_url, share_count, like_count, comment_count, total_count,commentsbox_count, comments_fbid, click_count FROM link_stat WHERE url='http://855read.com/article/category/global/id/97.html'";    


    $.getJSON(api_fb_ul, function(data){
       alert(data['like_count']);
    });

As we go to address api_fb_ul it show us array bellow:

{
   "data": [
      {
         "url": "http://855read.com/article/category/global/id/97.html",
         "normalized_url": "http://www.855read.com/article/category/global/id/97.html",
         "share_count": 812,
         "like_count": 5578,
         "comment_count": 838,
         "total_count": 7228,
         "commentsbox_count": 17,
         "comments_fbid": "1196527890387837",
         "click_count": 0
      }
   ]
}

Solution

  • You needed to explore the response a little further :)

    You can access what you want by doing this:

    var api_fb_ul = "https://graph.facebook.com/fql?q=SELECT url, normalized_url, share_count, like_count, comment_count, total_count,commentsbox_count, comments_fbid, click_count FROM link_stat WHERE url='http://855read.com/article/category/global/id/97.html'";
    
    $.getJSON(api_fb_ul, function(response) {
      console.log(response.data[0].like_count);
    });
    

    To show all keys/values:

    $.getJSON(api_fb_ul, function(response) {
      Object.keys(response.data[0]).forEach(function(key, index) {
        console.log(key, response.data[0][key]);
      });
    });