美化JSON、JSON字符串

 


dataConversion("{'a':1}")
function dataConversion (data) { let convert; try { JSON.parse(data); convert = this.formaterJSON(data, false).data; } catch (error) { convert = data; } return convert === undefined ? '' : convert; }
function nFunc (fn) { const Fn = Function; return (new Fn('return ' + '' + fn + ''))(); }
function formaterJSON(txt, compress) { const indentChar = ' '; let data; if (/^\s*$/.test(txt)) { return; } try { data = this.nFunc(txt); } catch (e) { return txt; } const draw = []; const line = compress ? '' : '\n'; const nodeCount = 0; const maxDepth = 0; const isLast = true; const indent = 0; this.notify('', data, isLast, indent, false, draw, line, nodeCount, compress, indentChar, maxDepth); return { data: draw.join(''), nodeCount: nodeCount, maxDepth: maxDepth }; }
function notify(name, value, isLast, indent, formObj, data, line, nodeCount, compress, indentChar, maxDepth, tabc) { let tab = tabc || ''; nodeCount++; /* 节点计数 */ for (let i = 0; i < indent; i++) { tab += indentChar; } /* 缩进HTML */ tab = compress ? '' : tab; /* 压缩模式忽略缩进 */ maxDepth = ++indent; /* 缩进递增并记录 */ if (value && value.constructor === Array) { this.notityArray(name, value, isLast, indent, formObj, data, line, nodeCount, compress, indentChar, maxDepth, tab); } else if (value && typeof value === 'object') { this.notifyObj(name, value, isLast, indent, formObj, data, line, nodeCount, compress, indentChar, maxDepth, tab); } else { const val = this.handleNotifyValue(value); data.push(tab + (formObj ? '"' + name + '":' : '') + val + (isLast ? '' : ',') + line); } } function handleNotifyValue(value) { const val = typeof value === 'string' ? '"' + value + '"' : value; return val; } function notityArray(name, value, isLast, indent, formObj, data, line, nodeCount, compress, indentChar, maxDepth, tab) { /* 处理数组 */ data.push( tab + (formObj ? '"' + name + '":' : '') + '[' + line ); /* 缩进'[' 然后换行 */ for (let i = 0; i < value.length; i++) { this.notify(i, value[i], i === value.length - 1, indent, false, data, line, nodeCount, compress, indentChar, maxDepth); } data.push(tab + ']' + (isLast ? line : ',' + line)); /* 缩进']'换行,若非尾元素则添加逗号 */ } function notifyObj(name, value, isLast, indent, formObj, data, line, nodeCount, compress, indentChar, maxDepth, tab) { /* 处理对象 */ data.push( tab + (formObj ? '"' + name + '":' : '') + '{' + line ); /* 缩进'{' 然后换行 */ let len = 0; let i = 0; for (const key in value) { if (value.hasOwnProperty(key)) { len++; } } for (const key in value) { if (value.hasOwnProperty(key)) { this.notify(key, value[key], ++i === len, indent, true, data, line, nodeCount, compress, indentChar, maxDepth); } } data.push(tab + '}' + (isLast ? line : ',' + line)); /* 缩进'}'换行,若非尾元素则添加逗号 */ }

 

function dataConversion (data) {
    let convert;
    try {
        JSON.parse(data);
        convert = formaterJSON(data, false).data;
    } catch (error) {
        convert = data;
    }
    return convert === undefined ? '' : convert;
}

function formaterJSON(txt, compress) {
    const indentChar = '    ';
    let data;
    if (/^\s*$/.test(txt)) {
        return;
    }
    try {
        data = nFunc(txt);
    } catch (e) {
        return txt;
    }
    const draw = [];
    const line = compress ? '' : '\n';
    const nodeCount = 0;
    const maxDepth = 0;
    const isLast = true;
    const indent = 0;
    notify('', data, isLast, indent, false, draw, line, nodeCount, compress, indentChar, maxDepth);
    return {
        data: draw.join(''),
        nodeCount: nodeCount,
        maxDepth: maxDepth
    };
}
function notify(name, value, isLast, indent, formObj, data, line, nodeCount, compress, indentChar, maxDepth, tabc) {
    let tab = tabc || '';
    nodeCount++; /* 节点计数 */
    for (let i = 0; i < indent; i++) { tab += indentChar; } /* 缩进HTML */
    tab = compress ? '' : tab; /* 压缩模式忽略缩进 */
    maxDepth = ++indent; /* 缩进递增并记录 */
    if (value && value.constructor === Array) {
        notityArray(name, value, isLast, indent, formObj, data, line, nodeCount, compress, indentChar, maxDepth, tab);
    } else if (value && typeof value === 'object') {
        notifyObj(name, value, isLast, indent, formObj, data, line, nodeCount, compress, indentChar, maxDepth, tab);
    } else {
        const val = handleNotifyValue(value);
        data.push(tab + (formObj ? '"' + name + '":' : '') + val + (isLast ? '' : ',') + line);
    }
}
function handleNotifyValue(value) {
    const val = typeof value === 'string' ? '"' + value + '"' : value;
    return val;
}

function notityArray(name, value, isLast, indent, formObj, data, line, nodeCount, compress, indentChar, maxDepth, tab) {
  /* 处理数组 */
    data.push(
        tab + (formObj ? '"' + name + '":' : '') + '[' + line
    ); 
    /* 缩进'[' 然后换行 */
    for (let i = 0; i < value.length; i++) {
        notify(i, value[i], i === value.length - 1, indent, false, data, line, nodeCount, compress, indentChar, maxDepth);
    }
    data.push(tab + ']' + (isLast ? line : ',' + line)); /* 缩进']'换行,若非尾元素则添加逗号 */
}
function notifyObj(name, value, isLast, indent, formObj, data, line, nodeCount, compress, indentChar, maxDepth, tab) {
  /* 处理对象 */
    data.push(
        tab + (formObj ? '"' + name + '":' : '') + '{' + line
    ); 
    /* 缩进'{' 然后换行 */
    let len = 0;
    let i = 0;
    for (const key in value) {
        if (value.hasOwnProperty(key)) {
            len++;
        }
    }
    for (const key in value) {
        if (value.hasOwnProperty(key)) {
            notify(key, value[key], ++i === len, indent, true, data, line, nodeCount, compress, indentChar, maxDepth);
        }
    }
    data.push(tab + '}' + (isLast ? line : ',' + line)); /* 缩进'}'换行,若非尾元素则添加逗号 */
}

function nFunc (fn) {
    const Fn = Function;
    return (new Fn('return ' + '' + fn + ''))();
}

 

posted @ 2020-07-02 14:26  -e  阅读(708)  评论(0编辑  收藏  举报