Javascript和Jquery:UncaughtTypeError:无法读取属性';长度';未定义的

Javascript和Jquery:UncaughtTypeError:无法读取属性';长度';未定义的,javascript,jquery,json,Javascript,Jquery,Json,目前我正在写一个网站,其中一个功能是允许用户输入一张牌的名称(游戏的炉石),并将其添加到网站上的牌组中。我使用的是来自 ,但我在查询这些数据时遇到问题 现在我有: // On user submission, searches for the inputted card name and places it into // the deck if a match is found $("#cardSearch").submit(function(){ card = $("#user

目前我正在写一个网站,其中一个功能是允许用户输入一张牌的名称(游戏的炉石),并将其添加到网站上的牌组中。我使用的是来自 ,但我在查询这些数据时遇到问题

现在我有:

// On user submission, searches for the inputted card name and places it into
// the deck if a match is found
$("#cardSearch").submit(function(){   
    card = $("#userInput").val();
    console.log("Card searched: " + card);
    $.getJSON("http://hearthstonejson.com/json/AllSets.json",function(cards){
       // cards = JSON.parse(cards);
        console.log(cards);
        $.each(cards.name, function(key, val){ 
            if (val.name == card) {
                console.log("Success, I found :" + val.name);
                return;
            }
        });   
    }   
    );
});
我的错误是Uncaught TypeError:无法读取未定义的属性'length',引用jquery最新版本的第631行

我想也许我必须解析数据,正如你在中间看到的注释代码行。但是,当我添加该行时,我会得到一个不同的错误:

未捕获的语法错误:意外标记o

我不知道该从哪里着手


谢谢你的帮助-我对这个很陌生

错误表示$。每个都无法以数组形式访问对象,这意味着它没有length属性

您的代码应该是:

$.each(cards, function(key, val)
试试这个:

var card = $("#userInput").val();
$.getJSON("http://hearthstonejson.com/json/AllSets.json", function (cards) {
    //Returns a JSON object where cards are grouped into something like this Basic, Credits, Debug etc
    //Each of these groups contains an array of cards
    //So the first step is to loop through the groups and inside this loop through the array of cards.
    $.each(cards, function (key, val) {
        //Here val is an array of cards
        //In order to get each card we have loop through val.
        $.each(val, function (index, data) {
            //Here data is refferd to as individual card
            if (data.name == card) {
                console.log("Success, I found :" + data.name);
                return;
            }
        });
    });
});
JSON卡:

Object {Basic: Array[212], Credits: Array[17], Debug: Array[39], Expert: Array[392], Missions: Array[37]…}
我们必须使用
卡。基本[索引]
才能获得特定的卡

Object {id: "GAME_004", name: "AFK", type: "Enchantment", text: "Your turns are shorter."}

这是

不是应该是$。每个(卡片、函数(键、值)都应该是吗?试着在代码开头将“卡片”定义为数组,然后看看它是否仍然给出“找不到未定义的长度”在控制台中,当您得到错误时,您应该能够将其展开,以便显示完整的堆栈跟踪。找到jQuery之外和您自己的代码中最近的位置;这将是您需要查看的地方。@KennyC将此作为一个答案发布。啊,有人实际查看了此API返回的JSON的详细结构。您可以应该解释一下为什么会有嵌套循环,或者显示一段JSON的摘录来证明它的合理性。