jsonify flask解析错误

时间:2016-03-25 18:01:53

标签: jquery json flask

为了简单起见,ajax收到了来自flask的jsonify响应,我找不到在html中呈现它的方法。 我试过了:

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()和其他东西,但没有任何效果。

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

将您的代码更新为

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

答案 1 :(得分:0)

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

应该是

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