JSON.stringify output to div in pretty print way

后端 未结 12 2400
野趣味
野趣味 2020-11-29 18:17

I JSON.stringify a json object by

result = JSON.stringify(message, my_json, 2)

The 2 in the argument above is su

12条回答
  •  庸人自扰
    2020-11-29 18:38

    My proposal is based on:

    • replace each '\n' (newline) with a
    • replace each space with  

    var x = { "data": { "x": "1", "y": "1", "url": "http://url.com" }, "event": "start", "show": 1, "id": 50 };
    
    
    document.querySelector('#newquote').innerHTML = JSON.stringify(x, null, 6)
         .replace(/\n( *)/g, function (match, p1) {
             return '
    ' + ' '.repeat(p1.length); });

提交回复
热议问题