user3155632
user3155632

Reputation: 484

how do i parse json value and get id into variable

hello i am having this code of jquery:

var fbuid = zuck;
var fbjson = $.getJSON("https://graph.facebook.com/"+fbuid);

how to get the id from json directly into var :

{
id: "4",
first_name: "Mark",
gender: "male",
last_name: "Zuckerberg",
link: "https://www.facebook.com/zuck",
locale: "en_US",
name: "Mark Zuckerberg",
username: "zuck"
}

all i would like to get id from json to var as below:

var fbjson.id;

how i do it using jquery?

Upvotes: 0

Views: 839

Answers (1)

Drew Dahlman
Drew Dahlman

Reputation: 4972

So you're close but you've got some things you need to adjust. Ajax is async which means that you're waiting on a server response. You need to fill your data once you have that piece of data. Note you will not be able to reference fbjson until AFTER the getJSON has fired and completed.

According to the jQuery documentation for getJSON

you need to have a callback similar to this -

var fbuid = 'zuck';
var fbjson;

$.getJSON( "https://graph.facebook.com/"+fbuid, function( data ) {
  fbjson = data;
});

Notice I assign the fbjson in the callback to data which is the server response. now you can reference it as

fbjson.id

Upvotes: 2

Related Questions