我想在不使用JQuery或任何其他库的情况下使用普通JavaScript在HTML表格中显示JSON结果,我有如下JSON响应:
{
"topalbums": {
"album": [
{
"name": "Konvicted",
"playcount": "61503",
"mbid": "13141107-58e2-4984-bb90-55aa59fc03f6",
"url": "http://www.last.fm/music/Akon/Konvicted",
"artist": {
"name": "Akon",
"mbid": "1138a764-2212-4d0a-b02d-0dc14df91e08",
"url": "http://www.last.fm/music/Akon"
},
"image": [
{
"#text": "http://userserve-ak.last.fm/serve/34s/55550849.png",
"size": "small"
},
{
"#text": "http://userserve-ak.last.fm/serve/64s/55550849.png",
"size": "medium"
},
{
"#text": "http://userserve-ak.last.fm/serve/126/55550849.png",
"size": "large"
},
{
"#text": "http://userserve-ak.last.fm/serve/300x300/55550849.png",
"size": "extralarge"
}
],
"@attr": {
"rank": "1"
}
},
我想访问第一个图片网址,如上所示,我尝试使用它,如下所示,但没有工作:
var text;
for (i = 0; i < json.topalbums.album.length; i++)
{
text += json.topalbums.album[i].name + " "+json.topalbums.album[i].image[1].#text+"<br>";
}
有人可以建议一些解决方案吗?
答案 0 :(得分:1)
您可以使用["#text"]
来访问它:
var text;
for (i = 0; i < json.topalbums.album.length; i++)
{
text += json.topalbums.album[i].name + " "+json.topalbums.album[i].image[1]["#text"]+"<br>";
}