基本上,我将 JSON 结果转换为 html 并使用 $.each 它遍历多个键。例如,我正在撤回 facebook 帖子并遍历该帖子中的喜欢。
问题在于,当有多个“喜欢”时,一切都很好!虽然当只有 1 个“喜欢”时,“源”键会从结果集中删除,并且我的 javascript 会中断,因为我希望它会在那里。知道为什么 $.each 会跳过单个节点的级别吗?以下是我的代码:
* 查询 **
$.each(post.likes.item, function(i, like){
$(currentpost).find('div.cc_likes').append(like + ',');
console.log(like)
});
* JSON 结果 **
*单喜欢
likes": {
"item": {
"source": {
"cta": "Mary Smith",
"url": "http:\/\/www.facebook.com\/",
"photo": {
"image": "https:\/\/graph.facebook.com\/"
}
}
},
结果在控制台:
Object
cta: "MaryAnn Smith"
photo: Object
url: "http://www.facebook.com/"
*多个喜欢
"likes": {
"item": [
{
"source": {
"cta": "Bobby Carnes Sr.",
"url": "http:\/\/www.facebook.com",
"photo": {
"image": "https:\/\/graph.facebook.com\"
}
}
},
{
"source": {
"cta": "Jenna Purdy",
"url": "http:\/\/www.facebook.com\",
"photo": {
"image": "https:\/\/graph.facebook.com\"
}
}
},
{
"source": {
"cta": "Kevin Say",
"url": "http:\/\/www.facebook.com\",
"photo": {
"image": "https:\/\/graph.facebook.com\"
}
}
}
],
"count": "10",
"count_display": "10"
},
结果在控制台:
Object
source: Object
cta: "Kevin Smith"
photo: Object
url: "http://www.facebook.com/"