【问题标题】:Get json object with jquery without using the $.each使用 jquery 获取 json 对象而不使用 $.each
【发布时间】:2011-08-17 21:35:19
【问题描述】:

我要检索第一首曲目的状态

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

我有这个代码来检索每个轨道的信息

$.getJSON("http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=melroymusic&api_key=690e1ed3bc00bc91804cd8f7fe5ed6d4&limit=3&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>");
    });
});

要检索第一首曲目的正在播放状态,逻辑上我会这样做

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

function(data){
    if (data.recenttracks.track[0]['@attr'].nowplaying == true){
        $("#lastfmMenu").append("Now Playing");
    }
    else {
        $("#lastfmMenu").append("Just played");
    }
});

没有给我任何结果。

希望你能帮帮我。

【问题讨论】:

    标签: javascript jquery json api last.fm


    【解决方案1】:

    是不是因为nowplaying是字符串而不是bool,所以应该是

    .nowplaying == "true"?
    

    不在电脑上,所以我无法检查。

    【讨论】:

      【解决方案2】:

      您是否尝试先对其进行解析?试试这个(未经测试),

      function(data){
        **var parsedData = $.parseJSON(data)**
          if (parsedData.recenttracks.track[0]['@attr'].nowplaying == true){
            $("#lastfmMenu").append("Now Playing");
          }
          else {
            $("#lastfmMenu").append("Just played");
          }
      });
      

      【讨论】:

      • 其实就算解析了,还是会保持为字符串。所以大卫是对的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 2013-11-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      相关资源
      最近更新 更多