2014-06-13 110 views
0

誰能告訴我爲什麼我不能讓field.set_url出來嗎?jquery,Ajax和JSON解析undefined

http://jsfiddle.net/W4xBJ/

$(document).ready(function(){ 
    $("button").click(function(){ 
    $.getJSON("https://api.deckbrew.com/mtg/cards?color=red&color=blue&rarity=rare&name=fire",function(result){ 
     $.each(result, function(i, field){ 
     $("div").append(field.name + "<br>" + field.url + "<br> "+ field.id + "<br> "+ field.set_url + "<br>"); 
     }); 
    }); 
    }); 
}); 
+0

如果你打印出'editions'的第一個索引... field.editions [0] .set_url',會發生什麼? – entiendoNull

+0

我想你可能已經回答了這個問題,謝謝。 – Patrick2Go

回答

0

set_url不會出現在那裏的結果對象,至少不是第一個結果:

{ 
    "editions": [ 
    { 
     "store_url": "http://store.tcgplayer.com/magic/odyssey/ashen-firebeast?partner=DECKBREW", 
     "set_url": "https://api.deckbrew.com/mtg/sets/ODY", 
     "image_url": "https://image.deckbrew.com/mtg/multiverseid/29965.jpg", 
     "url": "https://api.deckbrew.com/mtg/cards?multiverseid=29965", 
     "price": { 
     "note": "store.tcgplayer.com allows you to buy cards from any of our vendors, all at the same time, in a simple checkout experience. Shop, Compare & Save with TCGplayer.com!", 
     "high": 103, 
     "median": 32, 
     "low": 14 
     }, 
     "set": "Odyssey", 
     "set_id": "ODY", 
     "rarity": "rare", 
     "artist": "Mark Tedin", 
     "multiverse_id": 29965, 
     "flavor": "\"I'm not sure which impresses me most, its thoroughness or its intensity.\"\n\n—Matoc, lavamancer", 
     "number": "174", 
     "layout": "normal" 
    } 
    ], 
    "formats": { 
    "vintage": "legal", 
    "legacy": "legal", 
    "commander": "legal" 
    }, 
    "toughness": "6", 
    "power": "6", 
    "text": "{1}{R}: Ashen Firebeast deals 1 damage to each creature without flying.", 
    "cost": "{6}{R}{R}", 
    "name": "Ashen Firebeast", 
    "id": "ashen-firebeast", 
    "url": "https://api.deckbrew.com/mtg/cards/ashen-firebeast", 
    "store_url": "http://store.tcgplayer.com/magic/odyssey/ashen-firebeast?partner=DECKBREW", 
    "types": [ 
    "creature" 
    ], 
    "subtypes": [ 
    "beast", 
    "elemental" 
    ], 
    "colors": [ 
    "red" 
    ], 
    "cmc": 8 
} 

也許你從意味着store_url,也許set_urleditions的數組中的對象?

UPDATE

要使用set_urleditions陣列內,只需迭代它像以前一樣:

裏面的第一個$.each,把這個:

$.each(field.editions, function (index, edition) { 
    // do something with edition.set_url 
}); 
+0

是的,我現在將如何添加版本內的數組? – Patrick2Go

+0

^我用'versions'訪問'set_url'的代碼更新了我的答案。 – spinningarrow

+0

也,函數(我,字段)....什麼是「我」引用或它的功能是什麼? – Patrick2Go

0

我有更新在的jsfiddle試試這個

[http://jsfiddle.net/W4xBJ/2/][1] 

其實你的關鍵是store_url而不是set_url。你使用了錯誤的鍵