Does jQuery have a JSON/Javascript object to HTML pretty print function similar to PHP's var_dump? If yes, what is it?
- Take a look at this SO Post stackoverflow.com/questions/323517/…Chris Wagner– Chris Wagner2010-05-04 19:43:27 +00:00Commented May 4, 2010 at 19:43
- possible duplicate of stackoverflow.com/questions/323517/…artlung– artlung2010-05-04 19:56:22 +00:00Commented May 4, 2010 at 19:56
- 1I looked at that before I posted.Fletcher Moore– Fletcher Moore2010-05-04 20:13:25 +00:00Commented May 4, 2010 at 20:13
3 Answers
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)
Comments
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" } } ] } */