如何将附加对象添加到现有 JSON 文件(即对象数组)中?
这是我的 JS 代码:
const fs = require("fs");
let Human = {
Name: "John",
age: 20,
};
Human = JSON.stringify(Human, null, 2)
fs.appendFile('users.json', Human, error);
function error(err) {
console.log("1");
}
这给出了输出:
[
{
"Name": "John",
"age": 20,
},
{
"Name": "John",
"age": 20,
}
]{
"Name": "John",
"age": 20,
}
但是我需要:
[
{
"Name": "John",
"age": 20,
},
{
"Name": "John",
"age": 20,
},
{
"Name": "John",
"age": 20,
}
]
如何让它正确写入数组?