在纯JavaScript中加载多个JSON文件

在纯JavaScript中加载多个JSON文件,javascript,json,local-files,Javascript,Json,Local Files,我是JavaScript新手。我已经了解了如何使用JSON.Parse从JSON文件创建对象,现在我需要将多个本地JSON加载到一个数组中。我在谷歌上搜索我的问题已经有一段时间了,但我发现的一切都与单个JSON文件有关 在没有jQuery等库的纯JavaScript中有没有办法做到这一点 注意:无需使用web服务器,否则代码将在本地运行。以下伪代码片段可能会帮助您- var myArray = []; for(... loop through your files ...) { myAr

我是JavaScript新手。我已经了解了如何使用JSON.Parse从JSON文件创建对象,现在我需要将多个本地JSON加载到一个数组中。我在谷歌上搜索我的问题已经有一段时间了,但我发现的一切都与单个JSON文件有关

在没有jQuery等库的纯JavaScript中有没有办法做到这一点


注意:无需使用web服务器,否则代码将在本地运行。

以下伪代码片段可能会帮助您-

var myArray = [];
for(... loop through your files ...) {
    myArray.push(JSON.parse(your_file);
}

以下伪代码段可能会帮助您-

var myArray = [];
for(... loop through your files ...) {
    myArray.push(JSON.parse(your_file);
}

您可以这样做:

var file1 = JSON.parse(file1);
var file2 = JSON.parse(file2);
var file3 = JSON.parse(file3);
var myFileArray = [file1, file2, file3];
// Do other stuff
// ....
// Add another file to the array
var file4 = JSON.parse(file4);
myFileArray.push(file4);
如果已经有一个未解析文件数组,可以执行以下操作:

var myFileArray = [];
for(var i=0; i<unparsedFileArray.length; i++){
    myFileArray.push(JON.parse(unparsedFileArray[i]));
}

您可以这样做:

var file1 = JSON.parse(file1);
var file2 = JSON.parse(file2);
var file3 = JSON.parse(file3);
var myFileArray = [file1, file2, file3];
// Do other stuff
// ....
// Add another file to the array
var file4 = JSON.parse(file4);
myFileArray.push(file4);
如果已经有一个未解析文件数组,可以执行以下操作:

var myFileArray = [];
for(var i=0; i<unparsedFileArray.length; i++){
    myFileArray.push(JON.parse(unparsedFileArray[i]));
}

为此,您需要首先获取实际文件。然后,您应该解析它们

// we need a function to load files
// done is a "callback" function
// so you call it once you're finished and pass whatever you want
// in this case, we're passing the `responseText` of the XML request
var loadFile = function (filePath, done) {
    var xhr = new XMLHTTPRequest();
    xhr.onload = function () { return done(this.responseText) }
    xhr.open("GET", filePath, true);
    xhr.send();
}
// paths to all of your files
var myFiles = [ "file1", "file2", "file3" ];
// where you want to store the data
var jsonData = [];
// loop through each file
myFiles.forEach(function (file, i) {
    // and call loadFile
    // note how a function is passed as the second parameter
    // that's the callback function
    loadFile(file, function (responseText) {
        // we set jsonData[i] to the parse data since the requests
        // will not necessarily come in order
        // so we can't use JSONdata.push(JSON.parse(responseText));
        // if the order doesn't matter, you can use push
        jsonData[i] = JSON.parse(responseText);
        // or you could choose not to store it in an array.
        // whatever you decide to do with it, it is available as
        // responseText within this scope (unparsed!)
    }
})
如果无法发出XML请求,还可以使用文件读取器对象:

var loadLocalFile = function (filePath, done) {
    var fr = new FileReader();
    fr.onload = function () { return done(this.result); }
    fr.readAsText(filePath);
}

为此,您需要首先获取实际文件。然后,您应该解析它们

// we need a function to load files
// done is a "callback" function
// so you call it once you're finished and pass whatever you want
// in this case, we're passing the `responseText` of the XML request
var loadFile = function (filePath, done) {
    var xhr = new XMLHTTPRequest();
    xhr.onload = function () { return done(this.responseText) }
    xhr.open("GET", filePath, true);
    xhr.send();
}
// paths to all of your files
var myFiles = [ "file1", "file2", "file3" ];
// where you want to store the data
var jsonData = [];
// loop through each file
myFiles.forEach(function (file, i) {
    // and call loadFile
    // note how a function is passed as the second parameter
    // that's the callback function
    loadFile(file, function (responseText) {
        // we set jsonData[i] to the parse data since the requests
        // will not necessarily come in order
        // so we can't use JSONdata.push(JSON.parse(responseText));
        // if the order doesn't matter, you can use push
        jsonData[i] = JSON.parse(responseText);
        // or you could choose not to store it in an array.
        // whatever you decide to do with it, it is available as
        // responseText within this scope (unparsed!)
    }
})
如果无法发出XML请求,还可以使用文件读取器对象:

var loadLocalFile = function (filePath, done) {
    var fr = new FileReader();
    fr.onload = function () { return done(this.result); }
    fr.readAsText(filePath);
}

我确信文件的循环也是一个问题-更不用说尝试在Chrometh中读取本地文件了。根据MDN,这似乎不正确,不支持URI作为参数,正如您对_文件的使用所暗示的那样。我确信文件的循环也是一个问题——更不用说尝试用Chrometh读取本地文件了。根据MDN的说法,它似乎不支持URI作为参数,正如您对_文件的使用所暗示的那样。即使jQuery是纯JavaScript,我想你想说纯JS或香草JS,或者在谈论浏览器时说原生JSAPI。如果你说的是浏览器。。。我认为您需要在访问本地文件方面做一些工作,这些文件应该在浏览器沙箱之外。即使jQuery是纯JavaScript,我认为您想说纯JS或香草JS,或者在谈论浏览器原生JS API时。如果你说的是浏览器。。。我想你需要做一些工作来访问本地文件,这些文件应该在浏览器沙盒之外。我认为XMLHTTPRequest可能不使用没有WebServer的本地文件我不确定IE是否也:-@MaxUdalov你也可以使用FileReaderobject@royhowie有什么方法可以递归地获取文件路径吗?就像我有10000个文件,它们的路径应该添加到myFiles数组/@MaxUdalov explain。您需要生成每个文件路径吗?我认为XMLHTTPRequest可能不与没有WebServer的本地文件连接我不确定是否与IE一起:-@MaxUdalov您也可以使用FileReaderobject@royhowie有什么方法可以递归地获取文件路径吗?就像我有10000个文件,它们的路径应该添加到myFiles数组/@MaxUdalov explain。您需要生成每个文件路径吗?根据MDN,这似乎不正确,不支持您使用fileX时所建议的URI作为参数。根据MDN,这似乎不正确,不支持您使用fileX时所建议的URI作为参数。