Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery只输出JSON的第一次迭代_Jquery_Json_Each - Fatal编程技术网

jquery只输出JSON的第一次迭代

jquery只输出JSON的第一次迭代,jquery,json,each,Jquery,Json,Each,使用此代码: $.each(data.toptracks.track, function(index, item){ $('.lastfm').append('<figure class="clearfix"><p><a href="'+item.url+'">'+item.name+'</a>'+item.artist.name+'</p><div class="vinyl"><img class="reco

使用此代码:

$.each(data.toptracks.track, function(index, item){
    $('.lastfm').append('<figure class="clearfix"><p><a href="'+item.url+'">'+item.name+'</a>'+item.artist.name+'</p><div class="vinyl"><img class="recordLabel" src="' + item.image[3]['#text'] + '" /></div></figure>');
});
$。每个(data.toptracks.track,函数(索引,项){
$('.lastfm').append(''+item.artist.name+'

'); });
使用此URL查看JSON:

由于某种原因,只有第一次迭代(?)得到输出


jsiddle link:

可能是因为在第二次迭代中,
没有
图像
属性,因此
项.image[3]['#text']
将导致它崩溃

{
    "toptracks": {
        "track": [{
            "name": "Someone Like You",
            "duration": "284",
            "playcount": "1033",
            "mbid": "",
            "url": "http:\/\/www.last.fm\/music\/Adele\/_\/Someone+Like+You",
            "streamable": {
                "#text": "1",
                "fulltrack": "0"
            },
            "artist": {
                "name": "Adele",
                "mbid": "b0335a95-8a12-4c71-8149-5054ec847d04",
                "url": "http:\/\/www.last.fm\/music\/Adele"
            },
            "image": [{
                "#text": "http:\/\/userserve-ak.last.fm\/serve\/34s\/55125087.png",
                "size": "small"
            },
            {
                "#text": "http:\/\/userserve-ak.last.fm\/serve\/64s\/55125087.png",
                "size": "medium"
            },
            {
                "#text": "http:\/\/userserve-ak.last.fm\/serve\/126\/55125087.png",
                "size": "large"
            },
            {
                "#text": "http:\/\/userserve-ak.last.fm\/serve\/300x300\/55125087.png",
                "size": "extralarge"
            }],
            "@attr": {
                "rank": "1"
            }
        },
        {
            "name": "Somebody To Love Me (ft. Boy George & Miike Snow)",
            "duration": "298",
            "playcount": "1011",
            "mbid": "",
            "url": "http:\/\/www.last.fm\/music\/Mark%2BRonson%2B%2526%2BThe%2BBusiness%2BIntl\/_\/Somebody%2BTo%2BLove%2BMe%2B%2528ft.%2BBoy%2BGeorge%2B%2526%2BMiike%2BSnow%2529",
            "streamable": {
                "#text": "0",
                "fulltrack": "0"
            },
            "artist": {
                "name": "Mark Ronson & The Business Intl",
                "mbid": "146a66b3-5544-4b7e-9b69-9b53ee30746a",
                "url": "http:\/\/www.last.fm\/music\/Mark%2BRonson%2B%2526%2BThe%2BBusiness%2BIntl"
            },
/* NO IMAGE PROPERTY */
            "@attr": {
                "rank": "2"
            }
        },
// ...
由于没有
image
属性,当您执行
item.image
时,它返回
undefined
,因此
item.image[3]
正在尝试查找
undefined
上的
3
属性

为了解决这个问题,您可以测试每个深度级别,以确保属性存在

if( item.image && item.image[3] && item.image[3]['#text'] ) { 
    var txt = item.image[3]['#text']; 
} else { 
    var txt = ''; 
}

这是因为该数组中的第二项不包含
image[3]
property。

您能在JSFIDLE上托管它吗。。aJSON的最后一部分处于可运行状态+1,这正是我的控制台给我的:
无法读取未定义的
的属性“3”。哦,好吧,我该如何防止这种情况发生?@benhowdle89:您需要测试每个可能存在或不存在的属性
if(item.images&&item.image[3]&&item.image[3]['#text']]{var txt=item.image[3]['#text']}或者{var txt='''.}
…在我上面的评论中,我用
item.images
而不是
item.image
启动了
if()
。答案是正确的。