最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Load multiple JSON files in pure JavaScript - Stack Overflow

programmeradmin2浏览0评论

I am new to JavaScript. I have already understood how to create an object from a JSON-file with JSON.Parse() and now I need to load multiple local JSONs into an array. I've been googling my problem for a while, but everything that I found was related to single JSON files.

Is there any way to do this in pure JavaScript without any libraries like jQuery and etc.?

P.S.: There is no need to work with web-server or else, the code is running locally.

I am new to JavaScript. I have already understood how to create an object from a JSON-file with JSON.Parse() and now I need to load multiple local JSONs into an array. I've been googling my problem for a while, but everything that I found was related to single JSON files.

Is there any way to do this in pure JavaScript without any libraries like jQuery and etc.?

P.S.: There is no need to work with web-server or else, the code is running locally.

Share Improve this question edited Jul 6, 2020 at 7:16 Andreas 5,60810 gold badges47 silver badges55 bronze badges asked Feb 24, 2015 at 7:15 MaxMax 931 gold badge2 silver badges12 bronze badges 1
  • Even jQuery is pure JavaScript, I think you wanted to say plain JS or vanilla JS ( or when talking about browsers native JS API ). And if you are talking about browsers... I think you will need some work in accessing the local files which are supposed to be out of browser sandbox. – sarveshseri Commented Feb 24, 2015 at 7:22
Add a ment  | 

2 Answers 2

Reset to default 6

To do this, you need to first get the actual files. Then, you should parse them.

// 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 e 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!)
    }
})

If you can't make an XML Request, you can also use a file reader object:

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

You can do something like this:

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);

If you already have an array of un-parsed files you could do this:

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

评论列表(0)

  1. 暂无评论