如何 read/write 到 node.js 中的 JSON 文件
How to read/write to a JSON file in node.js
我是 node.js 的新手,我想知道如何(或者即使)我可以读取和写入 JSON 文件。我正在尝试创建一个易于访问的惩罚历史记录。
理想情况下,我希望能够按照以下方式创建一些东西:
{
"punishments": {
"users": {
"<example user who has a punishment history>": {
"punishment-1567346": {
"punishment-id": "1567346",
"punishment-type": "mute",
"punishment-reason": "<reason>"
},
"punishment-1567347": {
"punishment-id": "1567347",
"punishment-type": "ban",
"punishment-reason": "<reason>"
}
}
}
}
}
然后我就有办法访问格式化的惩罚历史。我真的不知道从哪里开始。
使用 NodeJS 文件系统https://nodejs.org/dist/latest-v14.x/docs/api/fs.html。
这里我使用了writeFileSync API to write to file and readFileSync来读取文件。另外,写入时不要忘记 JSON.stringify(data)
,因为您正在将数据写入 JSON 文件。
const fs = require("fs");
const path = require("path");
// Write Data
const data = {
"punishments": {
"users": {
"<example user who has a punishment history>": {
"punishment-1567346": {
"punishment-id": "1567346",
"punishment-type": "mute",
"punishment-reason": "<reason>"
},
"punishment-1567347": {
"punishment-id": "1567347",
"punishment-type": "ban",
"punishment-reason": "<reason>"
}
}
}
}
};
fs.writeFileSync(path.join(__dirname, "outputfilepath", "outputfile.json"), JSON.stringify(data), "utf8");
// Read data
const rData = fs.readFileSync(path.join(__dirname, "outputfilepath", "outputfile.json"), "utf8");
const jsonData = JSON.parse(rData);
这是工作示例,
https://repl.it/repls/OutrageousInbornBruteforceprogramming#index.js
您可以使用名为 fs
的 NodeJS built-in 库来执行 read/write 操作。
第 1 步 - 导入 fs
const fs = require('fs');
步骤 #2 - 读取文件
let rawdata = fs.readFileSync('punishmenthistory.json');
let punishments= JSON.parse(rawdata);
console.log(punishments);
现在您可以使用 punishments
变量来检查 JSON 文件中的数据。此外,您可以更改数据,但它现在只驻留在变量中。
步骤 #3 - 写入文件
let data = JSON.stringify(punishments);
fs.writeFileSync('punishmenthistory.json', data);
完整代码:
const fs = require('fs');
let rawdata = fs.readFileSync('punishmenthistory.json');
let punishments= JSON.parse(rawdata);
console.log(punishments);
let data = JSON.stringify(punishments);
fs.writeFileSync('punishmenthistory.json', data);
参考资料:
https://stackabuse.com/reading-and-writing-json-files-with-node-js/
你可以做这样的阅读:
const fs = require('fs')
function jsonReader(filePath, cb) {
fs.readFile(filePath, (err, fileData) => {
if (err) {
return cb && cb(err)
}
try {
const object = JSON.parse(fileData)
return cb && cb(null, object)
} catch(err) {
return cb && cb(err)
}
})
}
jsonReader('./customer.json', (err, customer) => {
if (err) {
console.log(err)
return
}
console.log(customer.address) // => "Infinity Loop Drive"
})
像这样写作:
const fs = require('fs')
const customer = {
name: "Newbie Co.",
order_count: 0,
address: "Po Box City",
}
const jsonString = JSON.stringify(customer)
fs.writeFile('./newCustomer.json', jsonString, err => {
if (err) {
console.log('Error writing file', err)
} else {
console.log('Successfully wrote file')
}
})
我是 node.js 的新手,我想知道如何(或者即使)我可以读取和写入 JSON 文件。我正在尝试创建一个易于访问的惩罚历史记录。 理想情况下,我希望能够按照以下方式创建一些东西:
{
"punishments": {
"users": {
"<example user who has a punishment history>": {
"punishment-1567346": {
"punishment-id": "1567346",
"punishment-type": "mute",
"punishment-reason": "<reason>"
},
"punishment-1567347": {
"punishment-id": "1567347",
"punishment-type": "ban",
"punishment-reason": "<reason>"
}
}
}
}
}
然后我就有办法访问格式化的惩罚历史。我真的不知道从哪里开始。
使用 NodeJS 文件系统https://nodejs.org/dist/latest-v14.x/docs/api/fs.html。
这里我使用了writeFileSync API to write to file and readFileSync来读取文件。另外,写入时不要忘记 JSON.stringify(data)
,因为您正在将数据写入 JSON 文件。
const fs = require("fs");
const path = require("path");
// Write Data
const data = {
"punishments": {
"users": {
"<example user who has a punishment history>": {
"punishment-1567346": {
"punishment-id": "1567346",
"punishment-type": "mute",
"punishment-reason": "<reason>"
},
"punishment-1567347": {
"punishment-id": "1567347",
"punishment-type": "ban",
"punishment-reason": "<reason>"
}
}
}
}
};
fs.writeFileSync(path.join(__dirname, "outputfilepath", "outputfile.json"), JSON.stringify(data), "utf8");
// Read data
const rData = fs.readFileSync(path.join(__dirname, "outputfilepath", "outputfile.json"), "utf8");
const jsonData = JSON.parse(rData);
这是工作示例, https://repl.it/repls/OutrageousInbornBruteforceprogramming#index.js
您可以使用名为 fs
的 NodeJS built-in 库来执行 read/write 操作。
第 1 步 - 导入 fs
const fs = require('fs');
步骤 #2 - 读取文件
let rawdata = fs.readFileSync('punishmenthistory.json');
let punishments= JSON.parse(rawdata);
console.log(punishments);
现在您可以使用 punishments
变量来检查 JSON 文件中的数据。此外,您可以更改数据,但它现在只驻留在变量中。
步骤 #3 - 写入文件
let data = JSON.stringify(punishments);
fs.writeFileSync('punishmenthistory.json', data);
完整代码:
const fs = require('fs');
let rawdata = fs.readFileSync('punishmenthistory.json');
let punishments= JSON.parse(rawdata);
console.log(punishments);
let data = JSON.stringify(punishments);
fs.writeFileSync('punishmenthistory.json', data);
参考资料: https://stackabuse.com/reading-and-writing-json-files-with-node-js/
你可以做这样的阅读:
const fs = require('fs')
function jsonReader(filePath, cb) {
fs.readFile(filePath, (err, fileData) => {
if (err) {
return cb && cb(err)
}
try {
const object = JSON.parse(fileData)
return cb && cb(null, object)
} catch(err) {
return cb && cb(err)
}
})
}
jsonReader('./customer.json', (err, customer) => {
if (err) {
console.log(err)
return
}
console.log(customer.address) // => "Infinity Loop Drive"
})
像这样写作:
const fs = require('fs')
const customer = {
name: "Newbie Co.",
order_count: 0,
address: "Po Box City",
}
const jsonString = JSON.stringify(customer)
fs.writeFile('./newCustomer.json', jsonString, err => {
if (err) {
console.log('Error writing file', err)
} else {
console.log('Successfully wrote file')
}
})