本文介绍了使用jQuery获取JSON Facebook Graph API用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用图形API来获取用户的一些基本信息,而不需要用户授权只是公开的细节。我试图使用jQuery和.getJSON获取数据并解析它,但是我很难想出如何访问我想要的键值对。

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.

我想要有一些类似

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尝试像item.name和一堆其他我认为是潜在的语法选项的东西,仍然不明确。

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

这种方法有什么问题我只是真的有使用JSON与twitter API的经验,上述方法可以正常工作。

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

示例:

PS。您有 $(#profile)的语法错误。append(< h3>+ name +< / h3>); 忘记 + 围绕名称

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

这篇关于使用jQuery获取JSON Facebook Graph API用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 19:45