0
我试图在我的JSON中识别一些空数组,并放置一个div。当脚本看到空数组时,它停止加载json。该代码是:Jquery/Ajax请求替换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”来代替,但它似乎没有加工。你能帮我么?
对不起彼得概念
证明,我不是在jQuery的大师并不能得到它。您能否请详细解释一下...... – qqruza 2013-03-27 22:11:34
谢谢彼得,这个问题只在显示“item.thumbnail”而不是“附件”时才显示为空......我添加了您的行,当附件为空时它只是不显示任何内容...当附件存在时,它显示图像... – qqruza 2013-03-27 22:25:25
请检查我链接的小提琴,它使用与上面完全相同的代码,除了我用'$('body' )',它似乎工作正常。 – 2013-03-27 22:29:16