我害怕再次挣扎。 我在网上访问了一个名为MTG JSON(http://mtgjson.com/)的资源,这是一个json文件,其中包含每个打印时每张Magic Gathering Card的信息。
现在我拥有的信息是卡片名称,我想获得多元宇宙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);
这些是让我头疼的问题,因为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英里。
答案 0 :(得分:0)
为什么你不使用那样的东西
cards.forEach(function(e){
if(e.name == "myName")
console.log(e.multiverseid);
});