Facebook API Javascript JSON response

17,918

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));
Share:
17,918
dominic
Author by

dominic

Updated on June 04, 2022

Comments

  • dominic
    dominic almost 2 years
    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?