我的.json文件存储在计算机的本地目录中。
data.json{
"foo": "bar",
"baz": "bat"
}
我想以某种方式将data.json作为参数加载到我的计算机本地存储的.js文件中。然后运行它。
app.jsvar foo = "bar",
baz = "bat"; // Somehow, I need to import these arguments from data.json
// Then do stuff with them...
app.js是一个纯粹的javascript文件(无法访问HTML"包装器"或任何JS库,如jQuery等)。
我想使用以下其中一项来调用app.js:
我该如何做到这一点?
答案 0 :(得分:2)
如果您正在使用节点,只需要json。
// app.js
var data = require('./data.json');
var foo = data.foo // etc
在命令行中,您可以运行
node app.js
当然,如果您的数据json位于不同的目录中,请相应地调整路径。你也可以允许它使用任何json作为参数(see here)