node.js 中是否需要 json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7163061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is there a require for json in node.js
提问by Coen
I would like to include a couple of JSON files in my JavaScript code that are in the same directory as my JavaScript source file.
我想在我的 JavaScript 代码中包含几个 JSON 文件,这些文件与我的 JavaScript 源文件位于同一目录中。
If I wanted to include another JavaScript file I could simply use require.
Now I'm using readFileSyncand __dirnameto get the JSON, which I think is an ugly way to do it.
如果我想包含另一个 JavaScript 文件,我可以简单地使用require. 现在我正在使用readFileSync并__dirname获取 JSON,我认为这是一种丑陋的方法。
Is there something similar for require that enables me to load a JSON file?
是否有类似的 require 使我能够加载 JSON 文件?
回答by goatslacker
As of node v0.5.x yes you can require your JSON just as you would require a js file.
从 node v0.5.x 开始,您可以像需要 js 文件一样需要 JSON。
var someObject = require('./somefile.json')
var someObject = require('./somefile.json')
In ES6:
在 ES6 中:
import someObject from ('./somefile.json')
import someObject from ('./somefile.json')
回答by serkan
JSON files don't require an explicit exports statement. You don't need to export to use it as Javascript files.
JSON 文件不需要显式导出语句。您无需导出即可将其用作 Javascript 文件。
So, you can use just requirefor valid JSON document.
因此,您可以仅require用于有效的 JSON 文档。
data.json
数据.json
{
"name": "Freddie Mercury"
}
main.js
主文件
var obj = require('data.json');
console.log(obj.name);
//Freddie Mercury
回答by Raynos
回答by rajmobiapp
Two of the most common
最常见的两种
First way :
第一种方式:
let jsonData = require('./JsonFile.json')
let jsonData = require('./JsonFile')// if we omitting .json also works
let jsonData = require('./JsonFile')// 如果我们省略 .json 也可以
OR
或者
import jsonData from ('./JsonFile.json')
Second way :
第二种方式:
1) synchronously
1) 同步
const fs = require('fs')
let jsonData = JSON.parse(fs.readFileSync('JsonFile.json', 'utf-8'))
2) asynchronously
2)异步
const fs = require('fs')
let jsonData = {}
fs.readFile('JsonFile.json', 'utf-8', (err, data) => {
if (err) throw err
jsonData = JSON.parse(data)
})
Note: 1) if we JsonFile.json is changed, we not get the new data, even if we re run require('./JsonFile.json')
注意:1)如果我们 JsonFile.json 被改变了,我们不会得到新的数据,即使我们重新运行 require('./JsonFile.json')
2) The fs.readFile or fs.readFileSync will always re read the file, and get changes
2) fs.readFile 或 fs.readFileSync 将始终重新读取文件,并获得更改
回答by Igor Litvinovich
You even can use requireof your JSON without specifying the extension .json. It will let you change the file extension to .jswithout any changes in your imports.
您甚至可以在不指定扩展名.json 的情况下使用JSON 的require。它将允许您将文件扩展名更改为.js,而无需对导入进行任何更改。
assuming we have ./myJsonFile.jsonin the same directory.
假设我们在同一目录中有./myJsonFile.json。
const data = require('./myJsonFile')
If in the future you'll change ./myJsonFile.jsonto ./myJsonFile.jsnothing should be changed in the import.
如果将来您将./myJsonFile.json更改为./myJsonFile.js,则导入时不应更改任何内容。

