Javascript 在 Node.js 中编写格式化的 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5693999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write formatted JSON in Node.js
提问by donald
I'm using Node.js to POST JSON to PostBin but the data is being wrongly formated (as you can see here: http://www.postbin.org/1cpndqw).
我正在使用 Node.js 将 JSON POST 到 PostBin,但数据格式错误(如您所见:http: //www.postbin.org/1cpndqw)。
This is the code I'm using for tesT:
这是我用于测试的代码:
var http = require('http');
var options = {
host: 'www.postbin.org',
port: 80,
path: '/1cpndqw',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.write(JSON.stringify({ a:1, b:2, c:3 }, null, 4));
req.end();
采纳答案by Charlie Martin
Well, primarily because JSON doesn't care how it's formatted, and you aren't doing any formatting yourself. What you need is a javascript prettyprinter, if you care, but the first question is "Why do you care?"
嗯,主要是因为 JSON 并不关心它是如何格式化的,而且您自己也没有进行任何格式化。如果你关心,你需要的是一个 javascript 漂亮的打印机,但第一个问题是“你为什么关心?”
Here's a prettyprintingcode from the Javascript Recipes.
这是来自 Javascript Recipes的漂亮打印代码。
Actually there's a whole bunch of different examples hereon SO.
实际上,有不同的例子一大堆这里的SO。
UPDATE
更新
Okay, so now it's doing what you want, let's ask if you're doing the right thing. As several people have pointed out, you needn't transmit those extra newlines and tabs, or spaces; the efficiency cost is small, probably in the neighborhood of 2-5 percent, but you never know when you might need a couple percent.
好的,现在它正在做你想做的事,让我们问问你是否在做正确的事情。正如一些人指出的那样,您不需要传输那些额外的换行符和制表符或空格;效率成本很小,可能在 2-5% 左右,但您永远不知道什么时候可能需要几个百分点。
On the other hand, I agree completely that it's a lot more convenient to be able to read the JSON output as prettyprinted text. But there's another solution -- you're still probably using a browser to look at these results, so instead of prettyprinting it for transmission, use a client-side prettyprinter. I use JSONView for Chromeand JSONViewin Firefox. Many debuggers will also prettyprint the JSON results for you as well.
另一方面,我完全同意能够将 JSON 输出读取为漂亮打印的文本要方便得多。但是还有另一种解决方案——您可能仍在使用浏览器来查看这些结果,因此不要使用客户端漂亮打印机来进行传输,而是使用客户端漂亮打印机。我用JSONView为Chrome和JSONView在Firefox。许多调试器也会为您打印 JSON 结果。
回答by Peter Lyons
Use JSON.stringify(object, null, 4)
where 4
is the number of spaces to use as the unit of indentation. You can also use "\t"
if you want tabs. This is actually part of the ECMAScript 5 specification, and is documented on MDN.
Use JSON.stringify(object, null, 4)
where4
是用作缩进单位的空格数。"\t"
如果需要选项卡,也可以使用。这实际上是 ECMAScript 5 规范的一部分,并记录在 MDN 上。
回答by Fritz Dodoo
I used a two step process that I found to work:
我使用了一个我发现有效的两步过程:
var output = JSON.parse(insert_json_here);
var print_to_file = JSON.stringify(output, null, "\t")
回答by Dave Dopson
You should check out underscore-cli- it's a command-line tool for inspecting and processing JSON data.
您应该查看underscore-cli- 它是一个用于检查和处理 JSON 数据的命令行工具。