我试图在我的 json 中识别一些空数组并放置一个 div 。当脚本看到空数组时,它会停止加载 json。代码是:
$.each(data.posts, function (i, item) {
var newstitle = item.title;
var newstmbn = item.thumbnail;
if (data !== undefined && data.posts !== undefined && data.posts.attachments !== undefined) {
$.each(item.attachments, function (i2, type) {
$('#news').append('<div>' + item.thumbnail + '</div>');
});
} else {
$.each(item.attachments, function (i2, type) {
$('#news').append('<div>' + type.images.thumbnail.url + '</div>');
});
}
output.append(news);
});
我的 json 看起来像:
{
"posts": [
{
"id": 914,
"title": "post1",
"thumbnail": "\/uploads\/url.jpeg",
"attachments": [
{
"images": {
"thumbnail": {
"url": "\/uploads\/url-150x92.jpeg"
}
}
}
]
},
{
"id": 915,
"title": "post1",
"thumbnail": "\/uploads\/url.jpeg",
"attachments": []
},
{
"id": 914,
"title": "post1",
"thumbnail": "\/uploads\/url.jpeg",
"attachments": [
{
"images": {
"thumbnail": {
"url": "\/uploads\/url-150x99.jpeg"
}
}
}
]
}
]}
所以你可以看到一些“附件”是空的,我想放置“item.thumbnail”,但它似乎不起作用。你能帮我么?