I’m working with the last.fm api to get artist images, and I’m getting JSON results where I need to check an ‘@attr’ value. Unfortunately, I can’t seem to access this value. The results look something like:
{"image":[{
"url":"http:\/\/www.last.fm\/music\/Undefined\/+images\/3040021",
"format":"jpg",
"sizes":{"size":{"#text":"http:...jpg","name":"original","width":"397","height":"397"},{"#text":"http:...jpg","name":"large","width":"126","height":"126"},]},
"@attr":{"official":"yes"}}
it’s that last value that I can’t get to…
any ideas?
I tried [‘@attr’] and it didnt’ seem to work – only returned undefined.
I’m doing an $.each(obj.image, function(){}) – and within i’m successfully getting this.url, this.format, etc – but i’m not having luck with this[‘@attr’]
Use the bracket notation member operator:
Then you can access the
officialproperty by:Or
Or
Edit: As Jonathan pointed out, you have issues with the JSON structure you post, I would recommend you to validate your JSON with a tool like JSONLint.
But I think that you mean something like this:
And with that JSON structure you can iterate it by: