将 JSON 对象写入文件

5年以前  |  阅读数:379 次  |  编程语言: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'

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8