Facebook API Javascript JSON response

前端 未结 3 2068
执笔经年
执笔经年 2021-02-06 17:48
function getUser()
{
    FB.api(\'/me\', function(response) {
       console.log(\'Response is \'+response);
       alert(\'Your name is \' + response.first_name);
              


        
3条回答
  •  情话喂你
    2021-02-06 18:17

    use JSON.stringify(response); it should print the entire object.

    use JSON.parse() or jQuery.parseJSON(); to parse and get any properties of the response object examples here : jsfiddle.net/epinapala/HrfkL and here : 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);​
    

    For multidimensional array as asked by the requester : THis should work : 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));
    

提交回复
热议问题