恐怕又要挣扎了。我访问了一个名为MTG JSON (http://mtgjson.com/)的在线资源,这是一个json文件,其中包含每次打印的聚集卡的每个魔法的信息。
现在,我所拥有的信息是卡名,我想要获得多维宇宙的id。如果此信息位于AllCards.json文件中,这将非常简单,但是它存储在AllSets.json中,这意味着我需要对对象进行一些挖掘。
我对javascript非常陌生,所以走到这一步已经是相当大的成就了,但我完全被难住了,需要帮助。
所以,首先是代码。我现在已经想出了这个来阅读这个文件:
$.getJSON("AllSets.json", function(json) {
console.log(json);
Sets = Object.keys(json);
i = 0;
while (i < Sets.length){
currentSet = Sets[i];
console.log(json.currentSet);
console.log(currentSet);
console.log(i);
i = i + 1;
}
});我已经开始在集合中移动,以便通过名字找到我当前所在的卡片。
问题是以下三行:
console.log(json.currentSet);
console.log(currentSet);
console.log(i);这些是最让我头疼的,因为我和currentSet都做得很好,返回当前集合和列表中该集合的当前位置。但是,json.currentSet将以undefined的形式返回。
对于那些对JSON结构感兴趣的人:
"SOI","releaseDate":"2016-04-08","border":"black","type":"expansion","block":"Shadows over Innistrad","booster":[["rare","mythic rare"],"uncommon","uncommon","uncommon","common","common","common","common","common","common","common","common","common","common","land","marketing"],"translations":{"de":"Schatten über Innistrad","fr":"Ténèbres sur Innistrad","it":"Ombre su Innistrad","es":"Sombras sobre Innistrad","pt":"Sombras em Innistrad","jp":"イニストラードを覆う影","cn":"依尼翠暗影","ru":"Тени над Иннистрадом","ko":"이니스트라드에 드리운 그림자"},"cards":[{"artist":"Chase Stone","cmc":3,"colorIdentity":["W"],"colors":["White"],"flavor":"\"We pray to Avacyn on high.\nOn snow-white wings fearless you fly.\nKeep safe our souls. Keep safe our lives.\nMay angels watch us from the skies.\"\n—Children's prayer","id":"c053a8c64d8c2ff2f17e4306344f353b46cd7352","imageName":"always watching","layout":"normal","manaCost":"{1}{W}{W}","multiverseid":409737,"name":"Always Watching","number":"1","rarity":"Rare","text":"Nontoken creatures you control get +1/+1 and have vigilance.","type":"Enchantment","types":["Enchantment"]},{"artist":"Joseph Meehan","cmc":8,"colorIdentity":["W"],"colors":["White"],"id":"80673365be1e340436928050e487afe0d2a70539","imageName":"angel of deliverance","layout":"normal","manaCost":"{6}{W}{W}","multiverseid":409738任何反馈、批评、建议等都将非常感谢,因为每次我认为我走了一英里,我就会看到前面还有10英里。
发布于 2016-06-08 23:18:48
为什么不使用这样的东西呢?
cards.forEach(function(e){
if(e.name == "myName")
console.log(e.multiverseid);
});https://stackoverflow.com/questions/37705785
复制相似问题