2017-12-23 10 views
0

Node.jsのようにJSONを印刷することができるかどうかわかりません。何らかの標準モジュールや何かがあれば。JSONをconsole.logと同様に出力すると、Node.jsに出力されます

enter image description here

着色がボーナスになるなど、これは、キーの間隔を持っており、それはあまりにも長い間だときには、新しい行になります。

JSON.stringify(object, null, 2) Node.jsのように、そこに何かもっと隠されたものがあるのか​​、それとも標準になっているのか疑問に思っています。ありがとうございました。

答えて

0

これはutil.inspect()を使用することによって達成可能であるように思わ:

const util = require('util'); 

const testJson = `{ 
    "id": "0001", 
    "type": "donut", 
    "name": "Cake", 
    "ppu": 0.55, 
    "batters": 
     { 
      "batter": 
       [ 
        { "id": "1001", "type": "Regular" }, 
        { "id": "1002", "type": "Chocolate" }, 
        { "id": "1003", "type": "Blueberry" }, 
        { "id": "1004", "type": "Devil's Food" } 
       ] 
     }, 
    "topping": 
     [ 
      { "id": "5001", "type": "None" }, 
      { "id": "5002", "type": "Glazed" }, 
      { "id": "5005", "type": "Sugar" }, 
      { "id": "5007", "type": "Powdered Sugar" }, 
      { "id": "5006", "type": "Chocolate with Sprinkles" }, 
      { "id": "5003", "type": "Chocolate" }, 
      { "id": "5004", "type": "Maple" } 
     ] 
}` 

x = JSON.parse(testJson); 
x.x = x; 
console.log(util.inspect(x, { colors: true })); 

with depth=2

optionsオブジェクトそれが再帰する方法を深く決定depthというパラメータを取ります。

console.log(util.inspect(x, { colors: true, depth: 3 })); 

私は、次を得る:

with depth=3

はそれがdepth: nullを渡す無限に再帰的にするために、私は3に増やした場合、デフォルト値は2です。デフォルト値はノードcliでも2と思われる。

関連する問題