2017-12-23 16 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

옵션은 오브젝트는 재귀 얼마나 깊은 결정 depth라는 매개 변수를 사용합니다.

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

나는 다음과 같은 얻을 :

with depth=3

그것이 depth: null을 통과 무한 재귀하려면 내가 3으로 증가하는 경우 기본값은 2입니다. 노드 cli에서도 기본값이 2 인 것처럼 보입니다.