Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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_Json - Fatal编程技术网

使用Javascript变量引用JSON键

使用Javascript变量引用JSON键,javascript,json,Javascript,Json,如何用javascript变量字符串替换“阿根廷”键 jQuery(document).ready(function() { $.getJSON('countries.json', function(data) { var output= data.Argentina[0].countryPhone1; alert(output); }); }); 下面是my.json数据文件的外观: { "Argentina": [{

如何用javascript变量字符串替换“阿根廷”键

jQuery(document).ready(function() {

    $.getJSON('countries.json', function(data) {

        var output= data.Argentina[0].countryPhone1;

        alert(output);

     });
});
下面是my.json数据文件的外观:

{
    "Argentina": [{
        "countryName":"Argentina",
        "countryPhone1":"1234"
    }],

    "Brazil": [{
        "countryName":"Brazil",
        "countryPhone1":"5678"
    }]
}
我尝试应用建议的解决方案,但我无法找到一种方法使其适应我的示例

var myString = 'Argentina';  /* or whatever you like */

var output = data[myString][0].countryPhone1;