Programmatic formatting solution:(编程格式化解决方案:)
The JSON.stringify
method supported by many modern browsers (including IE8) can output a beautified JSON string:
(许多现代浏览器(包括IE8)支持的JSON.stringify
方法可以输出一个美化的JSON字符串:)
JSON.stringify(jsObj, null, ""); // stringify with tabs inserted at each level
JSON.stringify(jsObj, null, 4); // stringify with 4 spaces at each level
(Demo: )http://jsfiddle.net/AndyE/HZPVL/
This method is also included with json2.js , for supporting older browsers.
(此方法也包含在json2.js中 ,用于支持旧版浏览器。)
Manual formatting solution(手动格式化方案)
If you don't need to do it programmatically, Try JSON Lint .
(如果您不需要以编程方式执行此操作,请尝试JSON Lint 。)
Not only will it prettify your JSON, it will validate it at the same time.(它不仅会美化你的JSON,它还会同时验证它。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…