我如何保存字符串在Node.js的一个.json
文件的数组:
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
example.json
[
"a", "b", "c", "d", "e", "f", "g", "h"
]
在节点JS,你可以这样做。
const fs = require('fs');
var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
const jsonContent = JSON.stringify(alphabet);
fs.writeFile("./alphabet.json", jsonContent, 'utf8', function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});