你知道有没有针对JavaScript的“JSON美化器”吗?
来自:
{"name":"Steve","surname":"Jobs","company":"Apple"}
至
{
"name" : "Steve",
"surname" : "Jobs",
"company" : "Apple"
}
举个例子
some_magic(jsonObj); // return beautified JSON
你知道有没有针对JavaScript的“JSON美化器”吗?
来自:
{"name":"Steve","surname":"Jobs","company":"Apple"}
至
{
"name" : "Steve",
"surname" : "Jobs",
"company" : "Apple"
}
举个例子
some_magic(jsonObj); // return beautified JSON
许多现代浏览器(包括 IE8)支持的 JSON.stringify
方法可以输出美观的 JSON 字符串:
JSON.stringify(jsObj, null, "\t"); // stringify with tabs inserted at each level
JSON.stringify(jsObj, null, 4); // stringify with 4 spaces at each level
演示:http://jsfiddle.net/AndyE/HZPVL/
这种方法也包含在json2.js中,以支持旧版浏览器。
如果您不需要以编程方式执行此操作,请尝试JSON Lint。 它不仅可以美化您的JSON,还可以同时验证它。