【发布时间】:2019-10-01 17:02:33
【问题描述】:
首先我在第一次运行之前截断 json 文件。
对于之后的每次运行,我都想继续附加到 json 文件中。第一次运行,因为数据为空,我得到 'SyntaxError: Unexpected end of JSON input'
public jsondata = (newdata: string) => {
var fs = require('fs');
fs.readFile('output.json', function (err, data) {
let json:any = [];
console.log('some data =' + data + '=');
if(data === '') {
json = JSON.parse(newdata);
json.push(newdata);
}
else {
json = JSON.parse(data);
json.push(newdata);
}
fs.writeFile('output.json', JSON.stringify(json, null, 4), (err) => {
if (err) throw err;
console.log('The json file has been saved');
});
});
}
【问题讨论】:
标签: javascript json typescript