Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How can I get the entire response like this below printed?

{
  "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"
}

Also by respose.name am getting the name of the user. How can I get the location in the parameter location as it is a JSON array?

like image 574
dominic Avatar asked Dec 11 '25 20:12

dominic


1 Answers

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));
like image 101
Eswar Rajesh Pinapala Avatar answered Dec 14 '25 10:12

Eswar Rajesh Pinapala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!