我正在一个小型服务器(NodeJS 环境)上工作以从 URL 和本地文件获取 JSON 数据以比较这些数据。
不幸的是,我正在使用“get-json”从 url 加载 JSON 并获取一些值 它不适用于本地文件。
我也用'request'试过了。
var getJSON = require('get-json')
var localpath = "./location/file.json"
getJSON(localpath, function(data, allowed_content) {
var a = allowed_content[0].age;
var b = allowed_content[1].age;
var c = allowed_content[2].age;
var d = allowed_content[5].age;
var e = allowed_content[6].age;
});
我不明白为什么本地路径不起作用。
最佳答案
您可以简单地使用 fetch功能
var localpath = "./location/file.json"
fetch(localpath)
.then(response => response.json())
.then(json => {
console.log(json);
});
关于javascript - 从本地文件获取 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57251469/