Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.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
如何获取json文件的javascript数组?_Javascript_Jquery_Arrays_Json - Fatal编程技术网

如何获取json文件的javascript数组?

如何获取json文件的javascript数组?,javascript,jquery,arrays,json,Javascript,Jquery,Arrays,Json,我有一个外部文件contacts.json。如何将其转换为javascript数组? 这是contacts.json的内容: { "ppl1":{ "Name":"Jhon", "Surname":"Kenneth", "mobile":329129293, "email":"jhon@gmail.com" }, "ppl2":{ "Name":"Thor", "Surname

我有一个外部文件contacts.json。如何将其转换为javascript数组?
这是contacts.json的内容:

{
    "ppl1":{
        "Name":"Jhon",
        "Surname":"Kenneth",
        "mobile":329129293,
        "email":"jhon@gmail.com"
    },
    "ppl2":{
        "Name":"Thor",
        "Surname":"zvalk",
        "mobile":349229293,
        "email":"thor@gmail.com"
    },
    "ppl3":{
        "Name":"Mila",
        "Surname":"Kvuls",
        "mobile":329121293,
        "email":"mila@gmail.com"
    }
}
回答

回答

已解决:

$.getJSON('contacts.json', function (json) {
var array = [];
for (var key in json) {
    if (json.hasOwnProperty(key)) {
        var item = json[key];
        array.push({
            name: item.Name,
            surname: item.Surname,
            mobile: item.mobile,
            email: item.email
        });            
    }
}
});
已解决:

$.getJSON('contacts.json', function (json) {
var array = [];
for (var key in json) {
    if (json.hasOwnProperty(key)) {
        var item = json[key];
        array.push({
            name: item.Name,
            surname: item.Surname,
            mobile: item.mobile,
            email: item.email
        });            
    }
}
});

此外部文件在您自己的服务器上吗?以下是一些有用的信息:。是的,此文件在我的服务器上。此外部文件在您自己的服务器上吗?以下是一些有用的信息:。是的,此文件在我的服务器上。字符串没有
.map
方法。字符串没有
.map
方法。
$.getJSON('contacts.json', function (json) {
var array = [];
for (var key in json) {
    if (json.hasOwnProperty(key)) {
        var item = json[key];
        array.push({
            name: item.Name,
            surname: item.Surname,
            mobile: item.mobile,
            email: item.email
        });            
    }
}
});