通过从命令行node
输入命令node will start a REPL。在此REPL中,有没有一种方法可以漂亮地打印JSON?
例如:从Bash:
Now using node v12.14.1 (npm v6.13.6)
:~/Code/tmp$ node
启动REPL:
Welcome to Node.js v12.14.1.
Type ".help" for more information.
> msg = { "hello":"world", "quantity": 1 }
执行JSON.stringify(msg)
产生:
'{"hello":"world","quantity":1}'
执行JSON.stringify(msg, null, 2)
产生:
'{\n "hello": "world",\n "quantity": 1\n}'
我想要的是:
{
"hello": "world",
"quantity": 1
}