2013-06-25 17 views
1

我的問題對你來說可能很簡單,但對我來說它非常困難,因爲我是jquery和json的新手。如何閱讀使用jquery的json fron facebook

此鏈接 https://graph.facebook.com/?ids=http://iphotogra.ph:4732/Images/ViewPhoto?photo_id=3538

將返回

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

我的問題是,我怎麼能訪問使用jQuery的股份和評論。

我想這

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); 
}); 

回答

3

你必須改變console.log(data.shares);console.log(data[link].shares);因爲shares特性在於它是由link變量引用的對象內。

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 ); 
}); 

演示:http://jsbin.com/ekuriv/1/edit

+0

謝謝!工作正常! :) –