Does jQuery have a JSON/javascript object to HTML pretty print function similar to PHP's var_dump?

前端 未结 3 1535
日久生厌
日久生厌 2021-02-05 11:44

Does jQuery have a JSON/Javascript object to HTML pretty print function similar to PHP\'s var_dump? If yes, what is it?

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-05 12:15

    Although the accepted answer is correct that jQuery does not have a pretty print feature for JSON, that feature is now included in out of the box javascript through JSON.stringify()'s space argument. To print to HTML, wrapping the output with

     
    will preserve the line spacing for readability purposes.

    var obj = {a:1, 'b':'foo', c:[false,'false',null, 'null', {d:{e:1.3e5,f:'1.3e5'}}]};
    var str = "
    " + JSON.stringify(obj, undefined, 4) + "
    "; /* Returns { "a": 1, "b": "foo", "c": [ false, "false", null, "null", { "d": { "e": 130000, "f": "1.3e5" } } ] } */

提交回复
热议问题