Для этого необходимо сначала получить фактические файлы. Затем вы должны разобрать их.
// 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);
}
Даже JQuery чистый JavaScript, я думаю, что ты хотел сказать, обычный JS или ванильный JS (или когда речь идет о собственных API-интерфейсах браузеров). И если вы говорите о браузерах ... Я думаю, вам понадобится некоторая работа по доступу к локальным файлам, которые, как предполагается, находятся вне песочницы браузера. –