Question

I'm trying to retrieve album artwork from the Last.FM API using JSON and jQuery Ajax, but I'm having trouble accessing the data structure, which looks like:

  "image":[
     {
        "#text":"http:\/\/userserve-ak.last.fm\/serve\/34s\/82993237.png",
        "size":"small"
     },
     {
        "#text":"http:\/\/userserve-ak.last.fm\/serve\/64s\/82993237.png",
        "size":"medium"
     },
     {
        "#text":"http:\/\/userserve-ak.last.fm\/serve\/174s\/82993237.png",
        "size":"large"
     },
     {
        "#text":"http:\/\/userserve-ak.last.fm\/serve\/300x300\/82993237.png",
        "size":"extralarge"
     },
     {
        "#text":"http:\/\/userserve-ak.last.fm\/serve\/_\/82993237\/Jake+Bugg.png",
        "size":"mega"
     }
  ],

Full URL: http://lab.starchcreative.co/zapp/albumartproxy.php?artist=Jake%20Bugg&album=Jake%20Bugg

This data is nested inside album as well, so I tried to do this to access the artwork URL for mega: albumArtURL = data.album.image.#text[4];

This returned an error as I had # in the item. So I just tried albumArtURL = data.album.image.text[4]; to no avail.

Can anyone help with what I'm doing wrong?

Was it helpful?

Solution

Have you tried to use bracket notation (instead of dot) for the problematic #text
and accessing the image's [4] array place before targeting the object key

albumArtURL = data.album.image[4]["#text"];

OTHER TIPS

   $.each(images.image, function (i, item) {
     var myvar = "text: " + item['#text'] + " size: " + item.size 
    })

here is a working example: fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top