Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在JSON文件中搜索。_Javascript_Jquery_Json_Search - Fatal编程技术网

Javascript 在JSON文件中搜索。

Javascript 在JSON文件中搜索。,javascript,jquery,json,search,Javascript,Jquery,Json,Search,我试图通过使用一个国家的ISO(cca2)代码搜索该国的首都 我正在做以下工作: var json = $.getJSON('Countries.json', function(data) { return data; }); var CapitalCity = getCapitalByCCA2(json, code.toString().toUpperCase()); alert(CapitalyCity); function getCapitalByCCA2(jso

我试图通过使用一个国家的ISO(cca2)代码搜索该国的首都

我正在做以下工作:

 var json =   $.getJSON('Countries.json', function(data)
{
       return data;
});

var CapitalCity = getCapitalByCCA2(json, code.toString().toUpperCase());

alert(CapitalyCity);

function getCapitalByCCA2(json, code) {
   for (var i = 0; i < json.length; i++) {
                if (json[i].cca2 === code.toString().toUpperCase()) {
                    return json[i].capital;
                    console.log(json[i]);
                }
            }
    return null;
};
问题是没有提取数据


我做错了什么?

您应该使用grep过滤json,如下所示:

var returnedData = $.grep(json, function (element, index) {
    return element.cca2 === code.toString().toUpperCase();
});
returnData将包含一个经过过滤的json数组

我在您的代码中没有看到
var code
的任何定义。

$。getJSON()
无法按预期工作,数据返回到变量中。相反,您需要做的是使用
$.getJSON()
提供回调来处理结果。尝试从成功回调中调用函数

// variable to store captial city
var CapitalCity;
// The country code you are filtering on
var countryCode = '...';
$.getJSON('Countries.json', function(data) {
    CapitalCity = getCapitalByCCA2(data, countryCode);      
});
实际上,您可以查看@MathieuLabrieParent提供的搜索建议,并完全删除您的函数。但是,我可能会更改为在数组迭代中使用
$.each()
,这样在找到匹配项时就可以跳出循环。可能是这样的:

// variable to store captial city
var CapitalCity;
// The country code you are filtering on
var countryCode = code.toString().toUpperCase();
$.getJSON('Countries.json', function(data) {
    $.each(data, function (index, element) {
        if (element.cca2 === countryCode) {
            CapitalCity = match.capital;
            // break the loop
            return false;
        }
    }); 
});

您不能从异步Ajax请求返回!阅读以下内容:获取问题第一部分的答案。您的JSON无效(末尾有一个额外的{)。用于使此有效。问题在于Ajax调用…您错过了。哎呀…您是对的。使用grep进行筛选仍然是一个好主意。@Brian请参阅您接受后对我的答案所做的更新。它具有更好的循环逻辑,以便您可以在符合条件时中断循环。
// variable to store captial city
var CapitalCity;
// The country code you are filtering on
var countryCode = code.toString().toUpperCase();
$.getJSON('Countries.json', function(data) {
    $.each(data, function (index, element) {
        if (element.cca2 === countryCode) {
            CapitalCity = match.capital;
            // break the loop
            return false;
        }
    }); 
});