How to pretty print JSON Object

JSON Object를 화면에 바로 보여주어야 할 때, 그냥 바로 출력을 하면 한줄로 길게 표현이 되기 때문에, 가독성이 떨어진다. 하지만 다음과 같이 하면 들여쓰기를 하여 JSON Object를 보기 좋게 프린트 할 수 있다.

1. Html

<pre><code class=JsonResult></code></pre>

2. Javascript

var jsonObject = [{name: "Gil-Dong", Age: 20, Birthday: new Date("2001-01-01")},{name: "Young-Hee", Age: 25, Birthday: null}];

$('.JsonResult').html(JSON.stringify(jsonObject, null, 3));

3.Result

[
   {
      "name": "Gil-Dong",
      "Age": 20,
      "Birthday": "2001-01-01T00:00:00.000Z"
   },
   {
      "name": "Young-Hee",
      "Age": 25,
      "Birthday": null
   }
]

글쓴이

thenewth

AI Research Engineer & Cloud Platform Developer

답글 남기기

아래 항목을 채우거나 오른쪽 아이콘 중 하나를 클릭하여 로그 인 하세요:

WordPress.com 로고

WordPress.com의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

Facebook 사진

Facebook의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

%s에 연결하는 중

This site uses Akismet to reduce spam. Learn how your comment data is processed.