테스트 사이트 - 개발 중인 베타 버전입니다

JSON Beautify

· 2년 전 · 684

온라인 서비스가 많지만 자체로 JSON Beautify를 만들어 보았습니다.

[code]

function jsonBeautify(json) {
    if (typeof json != 'string') {
        json = JSON.stringify(json, undefined, 2);
    }
    json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
        var cls = 'number';
        if (/^"/.test(match)) {
            if (/:$/.test(match)) {
                cls = 'key';
            } else {
                cls = 'string';
            }
        } else if (/true|false/.test(match)) {
            cls = 'boolean';
        } else if (/null/.test(match)) {
            cls = 'null';
        }
        return '<span class="' + cls + '">' + match + '</span>';
    });
}

[/code]

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

게시글 목록

번호 제목
17316
17315
17313
17312
17305
17304
17298
17294
17291
17285
17284
17279
17272
17271
17270
17262
17255
17248
17241
17238
17236
17220
17217
JavaScript 회원가입 2
17216
17214
17207
17205
17203
17201
17198