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

杀马特。学长 韩版系。学妹 提交于 2019-12-03 11:29:30

问题


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


回答1:


jQuery does not (out of the box).

However, James Padolsey created this prettyPrint which I really like.

Also, if you're using Firebug or Web Inspector (or similar), you can just type the object into the console, press return, and see a tree-dump of the object. To force a tree-view, call console.dir(obj)




回答2:


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 <pre> </pre> 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 = "<pre>" + JSON.stringify(obj, undefined, 4) + "</pre>";

/* Returns
{
    "a": 1,
    "b": "foo",
    "c": [
        false,
        "false",
        null,
        "null",
        {
            "d": {
                "e": 130000,
                "f": "1.3e5"
            }
        }
    ]
}
*/



回答3:


Using Jquery, you can have object.serialize() to output an object. This is similar to var_dump() in php or Zend_Debug::dump() in Zend.



来源:https://stackoverflow.com/questions/2768457/does-jquery-have-a-json-javascript-object-to-html-pretty-print-function-similar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!