Node的REPL /交互模式下的漂亮打印JSON

问题描述 投票:0回答:1

通过从命令行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
}
node.js read-eval-print-loop
1个回答
0
投票

[JSON.stringify返回未在节点控制台中解释的字符串。

如果要解释字符串,请使用console.log(JSON.stringify(msg, null, 2))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.