[英]Facebook API Javascript JSON response
function getUser()
{
FB.api('/me', function(response) {
console.log('Response is '+response);
alert('Your name is ' + response.first_name);
alert('Your last name is ' + response.last_name);
alert('Your Gender is ' + response.gender);
alert('Your status is '+response.username);
}
我怎样才能得到下面这样的完整回复?
{
"id": "blah blah",
"name": "blah blah",
"first_name": "blah blah",
"last_name": "blah blah",
"link": "https://www.facebook.com/blah blah",
"username": "blah blah",
"hometown": {
"id": "106442706060302",
"name": "Pune, Maharashtra"
},
"location": {
"id": "106377336067638",
"name": "Bangalore, India"
},
"bio": "╔══╗♫ ♪♫\n║██║\n║¨o•♫\n╚═|̲̅̅●̲̅̅|̅lιlllι ♫ I LoVe MuZiK!!\n\n█║▌│█│║▌║│█║▌│║\n® Copyright © 2012 ™\n█║▌│█│║▌║│█║▌│║\n\nReVoLt",
"gender": "male",
"relationship_status": "Single",
"timezone": 5.5,
"locale": "en_GB",
"verified": true,
"updated_time": "2012-06-15T05:33:31+0000",
"type": "user"
}
同样通过respose.name获取用户名。 如何获取参数位置中的位置,因为它是JSON数组?
使用JSON.stringify(response); 它应该打印整个对象。
使用JSON.parse()或jQuery.parseJSON(); 解析并获取响应对象示例的任何属性:jsfiddle.net/epinapala/HrfkL和这里:jsfiddle.net/epinapala/zfWWv/3
var obj2 = JSON.parse('{"id":"579156356","name":"Vishal Jethani","first_name":"Vishal","last_name":"Jethani","link":"https://www.facebook.com/vishal.jethani","username":"vishal.jethani","hometown":{"id":"106442706060302","name":"Pune, Maharashtra"},"location":{"id":"106377336067638","name":"Bangalore, India"},"bio":"bye bye to bad characters","gender":"male","relationship_status":"Single","timezone":5.5,"locale":"en_GB","verified":true,"updated_time":"2012-06-15T05:33:31+0000","type":"user"}');
alert("Parsing with Json : " + obj2.location.name);
对于请求者提出的多维数组:它应该工作: http : //jsfiddle.net/epinapala/WQcDg/
var obj2 = JSON.parse('{"work":[{"employer":{"id":"185998961446429","name":"Pvt Ltd"}}]}');
alert("Parsing with Json : " + JSON.stringify(obj2.work[0].employer.name));
我认为你必须使用FB Graph api。 如果你想要更多的信息,那么你必须这样使用
https://graph.facebook.com/579156356
https://graph.facebook.com/btaylor?access_token=AAAAAAITEghMBAHTRFEKjKcvFe1W2NJTu1Gsy20cNWEe0Dh98KoOhJEInZB1kXrMprNUaN6nq8FZA7YMZBvNVLwuz6pglh1SIBbZCnEECOAZDZD
我这样用。
function(evt){
console.log(evt.target.responseText);
var FB_obj = JSON.parse(evt.target.responseText);
console.log("fb id >>"+FB_obj.id);
console.log("fb name >>"+FB_obj.name);
}
使用Jquery获取使用纯javascript的Json方法,
我使用这个库来格式化日期, https://github.com/datejs/Datejs
$.getJSON("https://graph.facebook.com/5973249561/events/?access_token=YOUR_ACCESS_TOKEN&fields=id,name,description,start_time,place,cover,end_time&limit=4",
function(result){
for(x = 0; x < result.data.length; x++) {
var id = result.data[x].id;
var name = result.data[x].name;
var cover = result.data[x].cover.source;
var date = new Date(result.data[x].start_time);
var place = result.data[x].place.name;
var day = date.toString('dd');
var month = date.toString('MMM');
var url = 'https://facebook.com/'+id;
var time = date.toString("hh:mm tt");
$("#evt").append(name);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.