簡體   English   中英

如何在 Javascript 中將字符串數組保存到 JSON 文件?

[英]How to save an array of strings to a JSON file in Javascript?

如何將字符串數組保存到Node.js 中JSON文件:

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!");
}); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM