javascript - 如何将字符串数组保存到 Javascript 中的 JSON 文件?
问题描述
如何将字符串数组保存到Node.jsJSON
中的文件中:
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
例子.json
[
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h"
]
解决方案
在 Node 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!");
});
推荐阅读
- android - 如何检查引导加载程序是否已解锁
- android - 应用程序截图上传到 Google Play 商店后变得模糊
- javascript - RxJS combineLatest 不等待源 observables 发射?
- fiware - 上传小部件和混搭后,我的资源列表中没有显示资源
- python - 如何使用opencv python从底部裁剪图像
- imagemagick - 如何在imagemagick中转换灰度bmp
- javascript - Snap SVG 是否有任何类似的功能
? - git - git pull from origin 分支的奇怪问题
- magento2 - 在 Magento 2 中通过愿望清单产品 ID 获取客户详细信息
- python - 如何将每行的第一列数据添加到对应行中由某些特定字符串或字符标记的每列的头部?