这是我的示例数据:
http://api.setlist.fm/rest/0.1/setlist/4bf763f6.json

我只是在编写一个可打印此页面详细信息的节点应用程序。我关心的是布景,布景和歌曲。

var sets = setlist.sets
res.write(JSON.stringify(sets)) // THIS SHOWS THE CORRECT DATA
var numSets = Object.keys(sets).length;
for(var i = 0; i < numSets; i++){
    res.write("\nsets " + i);
    var set = sets.set[i];
    console.log(Object.getOwnPropertyNames(set))
    var numSet = Object.keys(set).length;
    res.write(JSON.stringify(set))
    for(var j = 0; j < numSet; j++){
        res.write("\nset " + (j+1) + " of " + numSet);
        var song = set.song;
        console.log(Object.getOwnPropertyNames(song))
        numSong = Object.keys(song).length;
        for(var k = 0; k < numSong; k++){
            res.write("\n song " + j + "-" + k);
            res.write("\n     "+JSON.stringify(song[k]["@name"]));
        }
    }
}


我得到的是:

set 1 of 1
 song 0-0
     "Lift Me Up"
 song 0-1
     "Hard to See"
 song 0-2
     "Never Enough"
 song 0-3
     "Got Your Six"
 song 0-4
     "Bad Company"
 song 0-5
     "Jekyll and Hyde"
 song 0-6
     "Drum Solo"
 song 0-7
     "Burn MF"
 song 0-8
     "Wrong Side of Heaven"
 song 0-9
     "Battle Born"
 song 0-10
     "Coming Down"
 song 0-11
     "Here to Die"


集合中有两个歌曲元素:(很抱歉,没有代码块,否则不会包装)

{
    "set": [{
        "song": [{
            "@name": "Lift Me Up"
        }, {
            "@name": "Hard to See"
        }, {
            "@name": "Never Enough"
        }, {
            "@name": "Got Your Six"
        }, {
            "@name": "Bad Company",
            "cover": {
                "@disambiguation": "British blues-rock supergroup",
                "@mbid": "0053dbd9-bfbc-4e38-9f08-66a27d914c38",
                "@name": "Bad Company",
                "@sortName": "Bad Company",
                "@tmid": "734487",
                "url": "http://www.setlist.fm/setlists/bad-company-3bd6b8b0.html"
            }
        }, {
            "@name": "Jekyll and Hyde"
        }, {
            "@name": "Drum Solo"
        }, {
            "@name": "Burn MF"
        }, {
            "@name": "Wrong Side of Heaven",
            "info": "Acoustic"
        }, {
            "@name": "Battle Born",
            "info": "Acoustic and Electric"
        }, {
            "@name": "Coming Down"
        }, {
            "@name": "Here to Die"
        }]
    }, {
        "@encore": "1",
        "song": [{
            "@name": "Under and Over It"
        }, {
            "@name": "Burn It Down"
        }, {
            "@name": "The Bleeding"
        }]
    }]
}


在Swift中,我只需要设置一个Dictionary,它就可以正常工作。 Javascript不是我的强项。为什么我看不到第二首歌?

最佳答案

setlist.set是两个对象组成的数组,一个对象包含“ regular”(?)歌曲,另一个对象包含encore信息。

看起来您正在将循环变量与其他对象/数组混合在一起,而不是迭代您认为要迭代的对象。

这是一个简化的版本,应显示您的期望:

// `sets` is an object containing (at least) `set` and `url` properties
var sets = setlist.sets;

// `set` is an array containing (in your example, two) objects
var set = sets.set;

for (var i = 0; i < set.length; ++i) {
  console.log('Set %d/%d', i+1, set.length);

  // `curSet` is an object containing a `song` property
  var curSet = set[i];

  // `songs` is an array of objects
  var songs = curSet.song;

  for (var j = 0; j < songs.length; ++j) {
    // `song` is an object containing properties like `@name` and possibly others
    var song = songs[j];

    console.log(' - Song: %s', song['@name']);
  }
}

09-17 07:48