0

我正在使用 Last.fm API 以JSON 格式返回数据,这工作正常。我正在使用user.getTopArtist() API 调用。

在页面加载时,会为每个艺术家创建一个 DIV 对象,其中包含来自 JSON 数据的相关详细信息。当用户使用 DIV 执行操作时,我基本上想交换图像 url 以显示更大的图像尺寸!

如何通过匹配它的存储值来查找/引用 JSON 对象?

例如,如果我需要匹配艺术家姓名“Kate Bush”,然后检索“超大”图像 url。我该怎么做?

数据结构如下所示:

{"topartists":{
  "artist":[{
    "name":"Kate Bush",
    "playcount":"20",
    "mbid":"4b585938-f271-45e2-b19a-91c634b5e396",
    "url":"http:\/\/www.last.fm\/music\/Kate+Bush",
    "image":[
      {"#text":"http:\/\/userserve-ak.last.fm\/serve\/34\/224740.jpg","size":"small"},
      {"#text":"http:\/\/userserve-ak.last.fm\/serve\/64\/224740.jpg","size":"medium"},
      {"#text":"http:\/\/userserve-ak.last.fm\/serve\/126\/224740.jpg","size":"large"},
      {"#text":"http:\/\/userserve-ak.last.fm\/serve\/252\/224740.jpg","size":"extralarge"},
      {"#text":"http:\/\/userserve-ak.last.fm\/serve\/500\/224740\/Kate+Bush.jpg","size":"mega"}
    ]
  }
}
4

2 回答 2

0

你用什么来解析 JSON?这是一个 jQuery 示例http://api.jquery.com/jQuery.parseJSON/

于 2011-03-21T21:07:02.217 回答
0

如果它只是为了这个特定的任务,那么这种$.each(data.topartists.artist[0].images)方法会奏效。

作为更通用的解决方案......看看http://goessner.net/articles/JsonPath/ - 这是JSON的XPath变体

于 2011-03-21T21:26:22.147 回答