【问题标题】:get json object with # in object name using jquery使用jquery在对象名称中获取带有#的json对象
【发布时间】:2011-10-28 05:33:06
【问题描述】:
  • 我正在使用 lastFm 的recentracks api:

www.last.fm/api/show?service=278

  • 我使用这些资源来了解如何使用它:

api.jquery.com/jQuery.getJSON/

www.hunlock.com/blogs/Mastering_JSON_%28_JavaScript_Object_Notation_%29

  • 我正在获取 json 格式:

http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=melroymusic&api_key=690e1ed3bc00bc91804cd8f7fe5ed6d4&limit=5&format=json&callback=?

我想检索艺术家的名字

{"track":[{"artist":{"#text":"deadmau5","mbid":""}

逻辑上我会使用

item.artist.#text

总代码:

$.getJSON("http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=melroymusic&api_key=690e1ed3bc00bc91804cd8f7fe5ed6d4&limit=5&format=json&callback=?",

function(data){
    $.each(data.recenttracks.track, function(i,item){
    $("#lastfmMenu").append("<div class='lastfmItem'><div class='lastfmText'>"+item.artist.#text+"</div>" + "<div class='lastfmDate'>"+item.name+"</div></div>");
    });
});

没用

我还没有找到可以用#检索对象的代码

我还看到了我也想使用的@attr 对象

 "@attr":{"nowplaying":"true"}

我不知道如何解决它,找不到任何关于它的东西

看看http://mellroy.com/ 看看它是如何工作的

提前谢谢你,真的很感激

【问题讨论】:

  • 你试过 item.artist["#text"]

标签: jquery json api object last.fm


【解决方案1】:

这里使用jQuery的each

var json = {"track":[{"artist":{"#text":"deadmau5","mbid":""}]};

$.each(json.track, function (b, c) {
    console.log('Index' + b, c.artist['#text']); // index 0 deadmau5
});

【讨论】:

  • 谢谢,这很简单,我猜@attr 也是如此? :['@attr']
  • 是的,只需使用字符串来访问这些属性。如果您认为它有效,请不要忘记将此标记为已回答。谢谢! meta.stackexchange.com/questions/16721/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-05
  • 1970-01-01
相关资源
最近更新 更多