最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何字符串数组保存在JavaScript中的JSON文件?

IT培训 admin 6浏览 0评论

如何字符串数组保存在JavaScript中的JSON文件?

我如何保存字符串在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!");
}); 

如何字符串数组保存在JavaScript中的JSON文件?

我如何保存字符串在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!");
}); 
发布评论

评论列表 (0)

  1. 暂无评论