0

我刚开始使用 lastfm 和 json。我可以得到我想在控制台中返回对象值的信息,但我不知道为什么我一直得到一个“未定义”的值。这是我所有的代码。谢谢!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
<head>
    <title>JSON LastFM API Test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
        $.getJSON("http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&artist=Bjork&api_key=690e1ed3bc00bc91804cd8f7fe5ed6d4&format=json&callback=?", function(data) {
            var html = '';
            $.each(data.artist, function(i, item) {
                html += "<p>" + item.name + "</p>";
                console.log(data);
            });
            $('#test').append(html);
        });

    </script>

    <div id="test"></div>
</body>

4

2 回答 2

1

返回的 JSON 似乎不是数组。

也许你可以试试

$.getJSON("http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&artist=Bjork&api_key="+ apikey+"&format=json&callback=?", function(data) {
        $('#test').append("<p>" + data.artist.name + "</p>");
});
于 2012-02-13T04:58:27.213 回答
0

如果它只是您想要的名称,在这种情况下非常简单:

   $.each(data, function(i, item) {
        html += "<p>" + item.name + "</p>";
        html += "<p>" + item.url + "</p>";
        console.log(data);
    });
    $('#test').append(html);

请为我提供更复杂的上下文,以便我可以帮助您。

于 2012-02-13T05:15:10.843 回答