Ядерный / физика элементарных частиц здесь.
Для улучшения вещей:
Еще одно предложение для улучшения вещей.
Use 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 recommend 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);
});
У вас довольно много проблем с форматированием в вашем фрагменте. Если они такие же в вашем фактическом JSON, у вас будут конфликты синтаксического анализа и объектной структуры с тем, что вы, вероятно, ожидаете.
{ /* 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 comma can cause parsing issues */
] /* no matching start */
},
"@attr": { "official":"yes" }
}