Javascript 使用jQuery进行jSON树搜索,错误

Javascript 使用jQuery进行jSON树搜索,错误,javascript,jquery,json,Javascript,Jquery,Json,我一直在使用有关stackoverflow的其他问题的代码,例如和 我得到错误uncaughttypeerror:cannotreadproperty'length'of undefined,当我尝试运行我的函数时,我可以在开发人员控制台中看到该错误。这个错误应该在jQuery文件本身中,而不是在我的代码中。(我知道ofc,但这仍然是我自己的错误) 以下是我试图执行的代码: function populate_api(){ var json = (function () {

我一直在使用有关stackoverflow的其他问题的代码,例如和

我得到错误
uncaughttypeerror:cannotreadproperty'length'of undefined
,当我尝试运行我的函数时,我可以在开发人员控制台中看到该错误。这个错误应该在jQuery文件本身中,而不是在我的代码中。(我知道ofc,但这仍然是我自己的错误)

以下是我试图执行的代码:

function populate_api(){
    var json = (function () {
        var json = null;
        $.ajax({ 
            'async': false,
            'global': false,
            'url': "test.txt",
            'dataType': "json",
            'success': function (data) {
                json = data;
            }
        });
        return json;
    })(); //^-from SOflow: https://stackoverflow.com/questions/2177548/load-json-into-variable
    //Put the JSON into a variable ---^


    $.each(json.result.matches.players, function(i, v){
        if (v.account_id == 38440257) {
            alert(v.hero_id);
                return;
        }
    }); //use variable to run a search on the JSON tree --^
}
包含Json的文件本身有很多信息,但这是我一直在测试的文件顶部的一个小区域:

{
    "result": {
        "status": 1,
        "num_results": 10,
        "total_results": 500,
        "results_remaining": 490,
        "matches": [
            {
                "match_id": 514348401,
                "match_seq_num": 468628758,
                "start_time": 1392061295,
                "lobby_type": 7,
                "players": [
                    {
                        "account_id": 38440257,
                        "player_slot": 0,
                        "hero_id": 42
                    },
...

再次快速总结。我正在匹配树中搜索帐户id为
38440257
的“英雄id”

这是因为
json.result.matches.players
undefined
jQuery。each()
没有任何检查第一个参数是否为
undefined
,而是假设您传递的是有效的,它可以访问的
length
属性

在JSON中,JSON
JSON.result.matches
是一个对象数组(每个对象代表一个匹配),每个对象都有一个
players
属性;数组本身没有
players
属性。您需要先迭代每一场比赛,然后再迭代每一名球员:

$.each(json.result.matches, function(index, match) {
    $.each(match.players, function(i, v) {
        // code here
    });
});
或者,只需检查特定比赛的球员(在本例中为第一场):


您还应该远离同步AJAX调用(这是一个荒谬的概念…),而是使用成功回调函数中的数据处理逻辑调用函数,传入JSON。

在使用JSON之前,您可以随时检查它是否未定义,如下所示:

if (typeof json != 'undefined') {
   // Do things with json
}
您可以将其包装在成功回调中,并在使用它之前检查是否定义了
数据
,同时跳过
返回json
部分:

function populate_api() {
    $.ajax({
        'async': false,
            'global': false,
            'url': "test.txt",
            'dataType': "json",
            'success': function (data) {
            if (typeof data != 'undefined') {
                $.each(data.result.matches, function (i, v) {
                    v.players.forEach(function(player){
                        if (player.account_id == 38440257) {
                            alert(player.hero_id);
                        }
                    });
                });
            }
        }
    });
}

您将在“success”函数之外获得json变量值,因此它将在ajax调用结束之前执行。尝试以这种方式进行更改:

'success': function (json) {
            return json;
        }
    });

你应该将你的迭代封装在一个函数中,并在成功时调用它。我刚刚测试了你上面发布的代码,我仍然得到同样的错误。所以我想我可以假设在json变量中输入一个值有问题?然而,我仍然不确定错误在哪里。我不熟悉。@k4kuz0您的问题正在JSON中迭代,请参阅我的答案。在JavaScript控制台中,您应该能够在成功回调后放置断点,以查看
数据和
JSON
包含的内容。感谢您的详细回复。我马上查一下。关于同步ajax调用,我只是使用了我在上面的答案中看到的代码,刚刚对它进行了测试,它工作得非常好。再次感谢你。
'success': function (json) {
            return json;
        }
    });