2016-03-25 139 views
0

为了简单起见,ajax从瓶子收到一个jsonify响应,我无法找到在html中呈现它的方式。 我试过了:jsonify烧瓶解析错误

success: function(json) { 
    $.each(json, function(index, element) { 
     $('#main').append(element.title); 
    }); 
} 

并得到“未定义”打印。

然后我尝试:

$('#main').append(json[index].name) 

,但没有得到印刷

这里的API返回的JSON:)

{ 
    "albums": [ 
    { 
     "artist_id": 19, 
     "composer_id": 3, 
     "id": 6, 
     "img": "The_Fine_Art_of_Self_Destruction.jpg", 
     "release_date": 2002, 
     "title": "the fine art of self destruction" 
    }, 
    { 
     "artist_id": 26, 
     "composer_id": 6, 
     "id": 22, 
     "img": "The_Blasters_%28album%29.jpg", 
     "release_date": 1981, 
     "title": "the blasters" 
    }, 
    { 
     "artist_id": 25, 
     "composer_id": 6, 
     "id": 25, 
     "img": "XMoreFunInTheNewWorld.jpg", 
     "release_date": 1983, 
     "title": "more fun in the new world" 
    }, 
    { 
     "artist_id": 27, 
     "composer_id": 8, 
     "id": 28, 
     "img": "220px-The_Angels_of_Light_Sing_%27Other_People%27.jpeg", 
     "release_date": 2005, 
     "title": "the angels of light sing 'other people'" 
    }, 
    { 
     "artist_id": 10, 
     "composer_id": 10, 
     "id": 32, 
     "img": null, 
     "release_date": 2008, 
     "title": "when the flood comes" 
    }, 
    { 
     "artist_id": 31, 
     "composer_id": 15, 
     "id": 40, 
     "img": "The_Willies.jpg", 
     "release_date": 2002, 
     "title": "the willies" 
    } 
    ] 
} 

我看了看很多东西像jQuery.parseJSON(和其他但没有任何工作。

谢谢你的帮助!

+0

'JSON [指数] .name'应该是'json.albums [指数] .name' –

回答

0
$.each(json, function(index, element) { 

应该

$.each(json.albums, function(index, element) { 
0

更新您的代码以

$.each(json.albums, function(index, element) { 
     $('#main').append(element.title); 
    });