将 JSON 写到文件 JSONToFile – JavaScript 实用代码片段

将 JSON 对象写入文件。

使用 fs.writeFile(),模板字面量 和 JSON.stringify()json 对象写入到 .json 文件中。

const fs = require('fs');
const JSONToFile = (obj, filename) =>
  fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2));
JSONToFile({ test: 'is passed' }, 'testJsonFile'); // writes the object to 'testJsonFile.json'

更多代码 JavaScript 实用代码片段 请查看 https://www.7psus5.com/30-seconds-of-code/

赞(0) 打赏
未经允许不得转载:WEBTian开发 » 将 JSON 写到文件 JSONToFile – JavaScript 实用代码片段

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

Tian开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏