Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get JSON Facebook Graph API User Info with jQuery

I am trying to use the graph API to get just some basic info about a user no need for authorization by the user just the public details. I am trying to use jQuery and the .getJSON to get the data and parse it but I am having a hard time trying to figure out how to access the key value pairs I want.

I want to have something like

var fburl = "http://graph.facebook.com/USER/callback=?"

$.getJSON(fburl, function(data){
  $.each(data, function(i,item){
     var name = item.user["name"];
     $("#profile").append("<h3>"name"</h3>");
  });
});

ive tried things like item.name and a bunch of other what I figured to be potential syntax options but still getting undefined.

Is there anything wrong with this approach I only really have experience using JSON with twitter API which works fine with the above approach.

When I console log data I get something like this

first_name: "First"
gender: "male"
id: "8977344590"
etc...
name: "Full Name"
like image 445
BillPull Avatar asked Jun 21 '11 20:06

BillPull


1 Answers

It doesn't return an array, but an object. You don't need to be looping over anything.

You can just get the name straight from the object:

$.getJSON(fburl, function(data){
     var name = data["name"];
     $("#profile").append("<h3>"+name+"</h3>"); 
});

example: http://jsfiddle.net/niklasvh/Wm9M3/

PS. you had a syntax error with $("#profile").append("<h3>"+name+"</h3>");as well (you forgot the + around name)

like image 168
Niklas Avatar answered Oct 12 '22 07:10

Niklas