【发布时间】:2013-12-06 02:14:44
【问题描述】:
有没有办法将 JSON 对象转储到文本文件以便从 Node 服务器进行调试?
我正在处理一个非常大的 JSON 对象,其中包含各种其他对象的数组。
理想情况下,生成的 txt 文件应该像这样正确格式化
{
type: 'Program',
body: [
{
type: 'VariableDeclaration',
declarations: [
{
type: 'AssignmentExpression',
operator: =,
left: {
type: 'Identifier',
name: 'answer'
},
right: {
type: 'Literal',
value: 42
}
}
]
}
]
}
解决方案:
var fs = require('fs');
var myData = {
name:'bla',
version:'1.0'
}
var outputFilename = '/tmp/my.json';
fs.writeFile(outputFilename, JSON.stringify(myData, null, 4), function(err) {
if(err) {
console.log(err);
} else {
console.log("JSON saved to ");
}
});
【问题讨论】:
-
这个问题应该有帮助:stackoverflow.com/questions/5670752/…
-
谢谢,这正是我想要的
标签: javascript json node.js