Possible Duplicate:
How can I beautify JSON programmatically?可能重复:如何以编程方式美化JSON ?
I know how to generate JSON from an object using JSON.stringify, or in my case the handy jquery-json from google code (https://github.com/krinkle/jquery-json).
我知道如何使用JSON从对象生成JSON。stringify,在我的例子中是来自谷歌代码的方便的jquery-json (https://github.com/krinkle/jquery-json)。
Now this works fine, but the output is hard to read for humans. Is there an easy way / function / whatever to output a neatly formatted json file?
现在这个方法很好,但是对于人类来说,输出却很困难。是否有一种简单的方法/函数/无论什么,可以输出格式整洁的json文件?
This is what I mean:
这就是我的意思:
JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}});
gives..
给. .
"{"a":1,"b":2,"c":{"d":1,"e":[1,2]}}"
I'd like something like this instead:
我想要的是这样的东西:
{
"a":1,
"b":2,
"c":{
"d":1,
"e":[1,2]
}
}
E.g. with newlines and tabs added. It's much easier to read for larger documents.
例如,增加了换行符和制表符。更大的文档更容易阅读。
I'd like to do this ideally without adding any huge libraries - e.g. not prototype or YUI or whatever.
我想在理想的情况下不添加任何大型库——例如,不添加prototype或YUI之类的。
1 个解决方案
#1
563
JSON.stringify
takes more optional arguments.
JSON。stringify采用更多可选参数。
Try:
试一试:
JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, 4); // Indented 4 spaces
JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, "\t"); // Indented with tab
From:
来自:
How can I beautify JSON programmatically?
如何以编程方式美化JSON ?
Should work in modern browsers, and it is included in json2.js if you need a fallback for browsers that don't support the JSON helper functions. For display purposes, put the output in a <pre>
tag to get newlines to show.
应该可以在现代浏览器中工作,并且它包含在json2中。如果浏览器不支持JSON帮助函数,则需要回退。出于显示目的,将输出放在
标记中,以获取要显示的新行。
#1
563
JSON.stringify
takes more optional arguments.
JSON。stringify采用更多可选参数。
Try:
试一试:
JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, 4); // Indented 4 spaces
JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, "\t"); // Indented with tab
From:
来自:
How can I beautify JSON programmatically?
如何以编程方式美化JSON ?
Should work in modern browsers, and it is included in json2.js if you need a fallback for browsers that don't support the JSON helper functions. For display purposes, put the output in a <pre>
tag to get newlines to show.
应该可以在现代浏览器中工作,并且它包含在json2中。如果浏览器不支持JSON帮助函数,则需要回退。出于显示目的,将输出放在
标记中,以获取要显示的新行。