admin管理员组文章数量:1222242
How can I save an array of strings to a JSON
file in Node.js:
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
example.json
[
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h"
]
How can I save an array of strings to a JSON
file in Node.js:
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
example.json
[
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h"
]
Share
Improve this question
edited Sep 9, 2020 at 15:49
Abraham
asked Jul 29, 2018 at 6:31
AbrahamAbraham
9,8756 gold badges55 silver badges58 bronze badges
1
|
1 Answer
Reset to default 16In Node js you can do like this.
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!");
});
本文标签: How to save an array of strings to a JSON file in JavascriptStack Overflow
版权声明:本文标题:How to save an array of strings to a JSON file in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739339432a2158863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
JSON.stringify
to turn the JSON object into a string (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…). – msbit Commented Jul 29, 2018 at 6:34