admin管理员组文章数量:1293739
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']
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']
Share Improve this question edited Nov 10, 2009 at 17:34 majman asked Nov 10, 2009 at 17:15 majmanmajman 2,06316 silver badges24 bronze badges 1- Actually I tried ['@attr'] and it didnt' seem to work - only returned undefined. I'm doing an $.each(obj.image) - and within i'm successfully getting this.url, this.format, etc - but i'm not having luck with this['@attr'] – majman Commented Nov 10, 2009 at 17:32
2 Answers
Reset to default 10Use the bracket notation member operator:
var value = obj[0]['@attr'];
Then you can access the official
property by:
value.official;
Or
obj[0]['@attr']['official'];
Or
obj[0]['@attr'].official;
Edit: As Jonathan pointed out, you have issues with the JSON structure you post, I would remend you to validate your JSON with a tool like JSONLint.
But I think that you mean something like this:
var obj = {
"image": [{
"url": "http:\/\/www.last.fm\/music\/Undefined\/+images\/3040021",
"format": "jpg",
"sizes": [{
"#text": "http:...jpg",
"name": "original",
"width": "397",
"height": "397"
},
{
"#text": "http:...jpg",
"name": "large",
"width": "126",
"height": "126"
}
],
"@attr": {
"official": "yes"
}
}]
};
And with that JSON structure you can iterate it by:
$.each(obj.image, function () {
alert(this['@attr'].official);
});
You have quite a few formatting issues in your snippet. If these are the same in your actual JSON, you're going to have parsing and object-structure conflicts from what you're probably expecting.
{ /* no matching end */
"images": [ /* no matching end */
{
"url":"http:\/\/www.last.fm\/music\/Undefined\/+images\/3040021",
"format":"jpg",
"sizes": { /* should this be an array instead? */
"size": {
"#text":"http:...jpg",
"name":"original",
"width":"397",
"height":"397"
},
{ /* missing key */
"#text":"http:...jpg",
"name":"large",
"width":"126",
"height":"126"
}, /* trailing ma can cause parsing issues */
] /* no matching start */
},
"@attr": { "official":"yes" }
}
本文标签: javascriptHow do I access an 39attr39 value in JSON with jQueryStack Overflow
版权声明:本文标题:javascript - How do I access an '@attr' value in JSON with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741584798a2386781.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论