jQuery 是否具有类似于 PHP 的 var_dump 的 JSON/javascript 对象到 HTML 的漂亮打印功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2768457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does jQuery have a JSON/javascript object to HTML pretty print function similar to PHP's var_dump?
提问by Fletcher Moore
Does jQuery have a JSON/Javascript object to HTML pretty print function similar to PHP's var_dump? If yes, what is it?
jQuery 是否具有类似于 PHP 的 var_dump 的 JSON/Javascript 对象到 HTML 的漂亮打印功能?如果是,那是什么?
回答by Matt
jQuery does not (out of the box).
jQuery 没有(开箱即用)。
However, James Padolsey created this prettyPrintwhich I really like.
然而,James Padolsey 创造了这个我非常喜欢的漂亮印刷品。
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)
此外,如果您使用 Firebug 或 Web Inspector(或类似工具),您只需在控制台中键入对象,按return,即可查看对象的树状转储。要强制树视图,请调用console.dir(obj)
回答by Michael.Lumley
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 javascriptthrough JSON.stringify()'s
space argument. To print to HTML, wrapping the output with <pre> </pre>
will preserve the line spacingfor readability purposes.
尽管接受的答案是正确的,jQuery 没有用于 JSON 的漂亮打印功能,但该功能现在通过JSON.stringify()'s
space 参数包含在开箱即用的 javascript 中。要打印到 HTML,将输出包装起来<pre> </pre>
将保留行间距以提高可读性。
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"
}
}
]
}
*/
回答by Jagadeesh
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.
使用 Jquery,您可能必须 object.serialize()
输出一个对象。这类似于var_dump()
在 php 或Zend_Debug::dump()
Zend 中。