javascript - 尝试使用 javascript api 在 linkedin 中的第一个连接上提取位置信息

标签 javascript json linkedin-api

我有从 Linkedin 中提取我的连接信息的工作代码。 但是,每当我尝试提取位置时,我都会得到奇怪的结果。

此代码有效,但您会在应该位于位置的位置看到“object OBJECT”。

<html lang="en">
<head>
<title>LinkedIn Industries</title>
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: xxxxxxx
onLoad: onLinkedInLoad
scope: r_network,r_emailaddress,r_fullprofile,r_basicprofile,r_contactinfo

</script>
</head>
<body>




<p>This example demonstrates how to retrieve a user's connections.  It also uses the LinkedIn auth events (load, login, logout) to control behavior.</p>

<!-- NOTE: be sure to set onLoad: onLinkedInLoad -->
<script type="text/javascript">
function onLinkedInLoad() {
  IN.Event.on(IN, "auth", function() {onLinkedInLogin();});
  IN.Event.on(IN, "logout", function() {onLinkedInLogout();});
}


function onLinkedInLogout() {
  setConnections({}, {total:0});
}

function onLinkedInLogin() {
  // here, we pass the fields as individual string parameters
  IN.API.Connections("me")
    .fields("id", "firstName", "lastName", "pictureUrl", "publicProfileUrl","location:(name)")
    .result(function(result, metadata) {
      setConnections(result.values, metadata);
    });
}

function setConnections(connections) {
  var connHTML = "<ul>";
  for (id in connections) {
    connHTML = connHTML + "<li><a href=\"" + connections[id].publicProfileURL + "\">";

    /* picture url not always there, must be defensive */
    if (connections[id].hasOwnProperty('pictureUrl')) {
      connHTML = connHTML + "<img align=\"baseline\" src=\"" + connections[id].pictureUrl + "\"></a>";
    }  else {
      connHTML = connHTML + "<img align=\"baseline\" src=\"http://static02.linkedin.com/scds/common/u/img/icon/icon_no_photo_80x80.png\"></a>";
    }

    connHTML = connHTML + "&nbsp;<a href=\"" + connections[id].publicProfileUrl + "\">";
    connHTML = connHTML + connections[id].firstName + " " + connections[id].lastName + "</a>";
    connHTML = connHTML + " (Location: " + connections[id].location + ")</li>";
  }

  connHTML = connHTML + "</ul>";
  document.getElementById("connectionsdata").innerHTML = connHTML;
}
</script>
<script type="IN/Login">
<div id="connectionstest">
  <p>Current User's Connections:</p>
  <div id="connectionsdata"></div>
</div>
</script>
</body>
</html>

主要问题确实存在于 location,在文档中,这应该替换为 location.name,但是,每当我这样做时,我什么也得不到,尽管我可以看到有一个带有 json 的 XHR 响应,它具有:

location:{"name":"都柏林等"

请帮忙解开隐藏地点的谜团

编辑:5/23/2013- 在 Chrome 的开发人员工具中,我可以看到一条消息说(未捕获的类型错误:无法读取未定义的属性“名称” - 希望这有助于有人帮助我

当我查看 chrome 开发人员的控制台时,有关于上述未捕获类型错误的更多详细信息:Uncaught TypeError: Cannot read property 'name' of undefined linkedconnect.html:55

setConnections linkedconnect.html:55

(匿名函数)linkedconnect.html:36 Sslac.Class.Constructor.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.b 框架:3242

(匿名函数)框架:173

Sslac.Class.Constructor.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.storedFailureResults 框架:3247

(匿名函数)框架:173

g框架:3135

pub.incoming framework:801

_window_onMessage 框架:566

最佳答案

也许您可以临时用连接数组真正包含的日志替换代码设置 html:

//connHTML = connHTML + "&nbsp;<a href=\"" + connections[id].publicProfileUrl + "\">";
//connHTML = connHTML + connections[id].firstName + " " + connections[id].lastName + "</a>";
//connHTML = connHTML + " (Location: " + connections[id].location + ")</li>";
console.log(connections[id]);

当您记录一个对象时,您可以单击它来检查它的属性,也许某些项目根本不包含位置。在这种情况下,您可以尝试:

function setConnections(connections) {
  var connHTML = "<ul>",
  tmpLocation;
  .....
  tmpLocation= (connections[id].location && connections[id].location.name)
    ?connections[id].location.name:"Unknown";
  connHTML = connHTML + " (Location: " + tmpLocation + ")</li>";

关于javascript - 尝试使用 javascript api 在 linkedin 中的第一个连接上提取位置信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16693942/

相关文章:

javascript - 在 Node.js 中解析 JSON 时遇到问题

ios - 使用适用于 iOS 的 SDK 在 Linkedin 上分享不起作用

javascript - 防止 Angular Directive(指令)多次编译

javascript - 在触摸开始时向主体添加类而不是 .scroll()

json - Swagger:从参数示例值中删除类属性

java - 如何将 JSON 数组转换为 Java 字符串数组

javascript - 从不同的数组大小创建一个表

javascript - 使用 jQuery 延迟 - when() 和 getJSON() 回调

python - 导入错误 - Linkedin API

python - 如何使用python将json从API插入到snowflake数据库?