0

i'm trying to pull some data from last.fm.

i get the following reply:

{
"tracks": {
    "track": [
        {
            "name": "Once Upon a Dream",
            "duration": "203",
            "loves": "738",
            "mbid": "92078817-2e04-4bcd-9c43-ebb9c2d1823c",
            "url": "http://www.last.fm/music/Lana+Del+Rey/_/Once+Upon+a+Dream",
            "streamable": {
                "#text": "0",
                "fulltrack": "0"
            },
            "artist": {
                "name": "Lana Del Rey",
                "mbid": "b7539c32-53e7-4908-bda3-81449c367da6",
                "url": "http://www.last.fm/music/Lana+Del+Rey"
            },
            "image": [
                {
                    "#text": "http://userserve-ak.last.fm/serve/34s/96432461.png",
                    "size": "small"
                },
                {
                    "#text": "http://userserve-ak.last.fm/serve/64s/96432461.png",
                    "size": "medium"
                },
                {
                    "#text": "http://userserve-ak.last.fm/serve/126/96432461.png",
                    "size": "large"
                },
                {
                    "#text": "http://userserve-ak.last.fm/serve/300x300/96432461.png",
                    "size": "extralarge"
                }
            ]
        }

And so on...

The problem lies when trying to access the image portion of the reply, the image object seems to have #text as variable name of the info i'm trying to access. a normal response.tracks.track[i].image[0].text obviously doesn't work.

is there some special way to access this variable ?

zvisofer
  • 1,346
  • 18
  • 41
Crimson
  • 87
  • 2
  • 11

2 Answers2

3

You can use the square bracket notation to access that variable like so:

response.tracks.track[i].image[0]['#text']
KylePlusPlus
  • 259
  • 3
  • 15
  • Oh my god, that should have been the next thing i tried. To much hours into this i guess. Worked perfectly, thanks! – Crimson Feb 05 '14 at 19:11
  • I know the feeling, sometimes you just need to take a break and come back to it. Glad I could help. – KylePlusPlus Feb 05 '14 at 19:14
2

It's just a key name inside of the object. You cannot access it via dot notation since it contains an invalid characters but you can use bracket notation. Here's a really simple example demonstrating this.

var foo = {
    '#bar': 'http://www.google.com/'
}

foo['#bar'] // will return a string value of http://www.google.com/

If your key contains any invalid characters it must be encased in a string. In your case, you're getting a JSON response which always contain string-encased key names.

Hope this helps!

Codist
  • 1,198
  • 2
  • 11
  • 28
  • i don't know if it's proper stackoverflow usage to get further into this but, is there a special reason for anyone to make the choice to give such a name to a variable ? – Crimson Feb 05 '14 at 19:27
  • I'd have to say not really other than trying to give the property name more of a meaning. I mean you can technically use any name you would like for a property name in a JavaScript object *(**as long as its encased within single quotes or double quotes**)* but the downside with this is, you cannot use dot notation to access that value anymore. You're forcing someone to use bracket notation which could be confusing for someone to read if they don't know where its coming from (ie thinking of it as an array). – Codist Feb 05 '14 at 20:25