【发布时间】:2010-06-29 15:27:51
【问题描述】:
我正在尝试使用 LastFM API 解析 JSON 提要,但 JSON 数组中返回的某些元素以 # 为前缀,我不知道如何引用。
Feed URL 是here,可以看到visualised here。
到目前为止,我的 jQuery 代码如下所示:
$.getJSON('http://ws.audioscrobbler.com/2.0/?method=geo.getevents&api_key=ca1599876cde298491941da8577de666&format=json&callback=?', function(data) {
$.each(data.events.event, function(i, item) {
html += "<li><a class='fm-event' href='" + item.url + "'><h4>" + item.title + "</h4>";
html += "<p>at " + item.venue.name + ", " + item.venue.location.city + "<br />";
html += "on " + item.startDate + "</p>";
html += "<img src='" + item.venue.image.text + "' />"; // This doesn't work. how do I do this?
html += "</a></li>";
});
$("#last-fm-events").append(html);
});
我基本上是遍历提要中的每个项目并动态构建一个列表,然后将其附加到 DOM。
我不知道如何获取提要中图片项目的 URL。不同的尺寸有不同的。图像元素的 JSON 如下所示:
"image": [
{
"#text": "http:\/\/userserve-ak.last.fm\/serve\/34\/2243904.gif",
"size": "small"
},
{
"#text": "http:\/\/userserve-ak.last.fm\/serve\/64\/2243904.gif",
"size": "medium"
},
{
"#text": "http:\/\/userserve-ak.last.fm\/serve\/126\/2243904.gif",
"size": "large"
},
{
"#text": "http:\/\/userserve-ak.last.fm\/serve\/252\/2243904.gif",
"size": "extralarge"
},
{
"#text": "http:\/\/userserve-ak.last.fm\/serve\/_\/2243904\/A38.gif",
"size": "mega"
}
]
}
但我不明白为什么数组中的文本元素以 # 为前缀,以及如何获取特定大小图像的 URL。任何帮助表示赞赏,因为我是 jQuery 初学者!谢谢。
【问题讨论】:
标签: javascript jquery json parsing