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"
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
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With