我正在处理 Facebook Javascript SDK 的结果,该对象的“附件”部分给我带来了麻烦。当查看该对象时,一切都很好,但是当我尝试在我的网站上显示它时,它说这些属性未定义。我解析错误吗?
这是对象:
{
"id":"xxx_xxx",
"from":{
"id":"xxxx",
"name":"User Name"
},
"to":{
"data":[{
"name":"Group Name",
"id":"xxxx"
}]
},
"picture":"url of image",
"link":"url",
"object_id":"Object ID",
"type":"photo",
"created_time":"2014-10-15T21:09:19+0000",
"updated_time":"2014-10-15T21:09:19+0000",
"attachments":{
"data":[{
"media":{
"image":{
"height":551,
"src":"Image Source",
"width":720
}
},
"target":{
"id":"742450405803347",
"url":"target URL"
},
"type":"photo",
"url":"URL"
}]
}
这是在网站上显示“附件”部分的代码(我知道它很乱,请忽略它):
post_div_html += '<p>------------------</p>\
<p><b>Media-Image-Source</b>: ' + feed.attachments.data[0] + '</p>\
<p><b>Target URL</b>: ' + feed.attachments.data[1] + '</p>\
<p><b>Type</b>: ' + feed.attachments.data[2] + '</p>\
<p><b>URL</b>: ' + feed.attachments.data[3] + '</p></div>';
现在,需要澄清的是,您所看到的结果导致“未定义”在我的网站上显示为值。我尝试过的所有其他操作都导致错误,提示“feed.attachments.data.whatever 未定义”。
有人对我需要如何展示这些东西有任何想法吗?
最佳答案
我想你想要的是......
var att = feed.attachments.data[0],
mediaImageSrc = att.media.image.src,
targetUrl = att.target.url,
type = att.type,
url = att.url;
我希望这是有道理的。
这是一个工作示例...
var feed = {
"id": "xxx_xxx",
"from": {
"id": "xxxx",
"name": "User Name"
},
"to": {
"data": [{
"name": "Group Name",
"id": "xxxx"
}]
},
"picture": "url of image",
"link": "url",
"object_id": "Object ID",
"type": "photo",
"created_time": "2014-10-15T21:09:19+0000",
"updated_time": "2014-10-15T21:09:19+0000",
"attachments": {
"data": [{
"media": {
"image": {
"height": 551,
"src": "Image Source",
"width": 720
}
},
"target": {
"id": "742450405803347",
"url": "target URL"
},
"type": "photo",
"url": "URL"
}]
}
};
var att = feed.attachments.data[0],
mediaImageSrc = att.media.image.src,
targetUrl = att.target.url,
type = att.type,
url = att.url;
post_div_html = '<p>------------------</p>\
<p><b>Media-Image-Source</b>: ' + mediaImageSrc + '</p>\
<p><b>Target URL</b>: ' + targetUrl + '</p>\
<p><b>Type</b>: ' + type + '</p>\
<p><b>URL</b>: ' + url + '</p></div>';
document.getElementById('out').innerHTML = post_div_html;
<output id="out"></output>
关于javascript - Facebook 对象的一部分显示为未定义,但它并非未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26394840/