JSON - 从网址获取Facebook总数

时间:2014-07-07 01:28:37

标签: javascript json facebook facebook-graph-api facebook-fql

此脚本从Facebook粉丝页面获取喜欢的数量。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
//Set Url of JSON data from the facebook graph api. make sure callback is set   with a '?' to overcome the cross domain problems with JSON
var url = "https://graph.facebook.com/tenniswarehouse?callback=?";

//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
$.getJSON(url,function(json){
    var html = "<ul><li>" + json.likes + "</li></ul>";
    //A little animation once fetched
    $('.facebookfeed').animate({opacity:0}, 500, function(){
        $('.facebookfeed').html(html);
    });
    $('.facebookfeed').animate({opacity:1}, 500);
});
});
</script>
</head>
<body>
<div class="facebookfeed">
<h2>Loading...</h2>
</div>
</body>
</html> 

Source

我试图让他从网址中获取喜欢,分享和评论的总数(&#34; total_count&#34;),但它没有用。

<script type="text/javascript">
$(function() {
//Set Url of JSON data from the facebook graph api. make sure callback is set with a '?' to overcome the cross domain problems with JSON
var url = "https://graph.facebook.com/fql?q=SELECT%20url,%20normalized_url,%20share_count,%20like_count,%20comment_count,%20total_count,commentsbox_count,%20comments_fbid,%20click_count%20FROM%20link_stat%20WHERE%20url=    %27http://www.google.com%27?callback=?";

//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
$.getJSON(url,function(json){
var html = "<ul><li>" + json.total_count + "</li></ul>";
//A little animation once fetched
$('.facebookfeed').animate({opacity:0}, 500, function(){
$('.facebookfeed').html(html);
});
$('.facebookfeed').animate({opacity:1}, 500);
});
});
</script>
</head>
<body>
<div class="facebookfeed">
<h2>Loading...</h2>
</div>
</body>
</html>

你能不能帮我把剧本变成&#34; total_count&#34;来自链接?

1 个答案:

答案 0 :(得分:2)

我想

var html = "<ul><li>" + json.data[0].total_count + "</li></ul>";

应该...确保从FQL中的URL中删除多余的空格。

看看这个小提琴:http://jsfiddle.net/e24ey/1/