heyjohnmurray
heyjohnmurray

Reputation: 303

lastfm JSON object returning "undefined"

i just started with lastfm and json. i can get the information i want to return object values in the console, but i can't figure out why i keep getting a value of "undefined". here's all my code. thanks!

<!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>

Upvotes: 0

Views: 346

Answers (2)

Johannes Staehlin
Johannes Staehlin

Reputation: 3720

If it's just the name you want to have it is pretty simple in this case:

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

Please provide me with a more complex context, so I can help you have that.

Upvotes: 0

ironchefpython
ironchefpython

Reputation: 3409

It appears the JSON returned is not an array.

Perhaps you can try

$.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>");
});

Upvotes: 1

Related Questions