如何在 Javascript 中将字符串数组保存到 JSON 文件?

2024-01-03

如何将字符串数组保存到JSON文件输入Node.js:

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!");
}); 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Javascript 中将字符串数组保存到 JSON 文件? 的相关文章

随机推荐