我在服务器[{"a":1}, {"a":2}]
上有此JSON,想知道是否无论如何最后都要添加一个对象,而不是是否在服务器上重写整个文件。我已经做出了解决方法,删除了括号,并在服务器加载后添加了它们。
newObj= {'a':3};
fs.appendFile(theServerFile, ',' + newObj, ...) ;
答案 0 :(得分:-1)
我将执行以下操作:
const fs = require('fs');
fs.readFile('./jsonFile.html', function read(err, data) {
if (err) {
throw err;
}
processFile( JSON.parse(data) );
});
function processFile(data) {
data.push(newObj);
fs.writeFileSync('./jsonFile.html', data);
}
检索当前内容,对其进行更新并再次写入。
希望这会有所帮助!