关于如何将JSON嵌入到HTML页面中并使用JSON格式化为人类可读样式的建议?例如,当您在浏览器中查看XML时,大多数浏览器都会显示XML格式(缩进,适当的换行符等)。对于JSON,我希望获得相同的最终结果。
颜色语法突出显示将是一项奖励。
谢谢
解决方案您可以使用带有未格式化JSON的JSON.stringify函数。它以格式化的方式输出它。
JSON.stringify({foo:sample,bar:sample},null,4)p> {foo:sample,bar:sample }现在数据是一种可读格式,您可以使用Google代码美化脚本。征税来给它加上颜色代码。
值得一提的是, IE7和旧版浏览器不支持JSON.stringify方法。
Any recommendations on how to embed JSON in an HTML page with the JSON formatted in a human readable style? For example, when you view XML in a browser, most browsers display the XML formatted (indented, proper line breaks, etc). I'd like the same end result for JSON.
Color syntax highlighting would be a bonus.
Thanks
解决方案You can use the JSON.stringify function with unformatted JSON. It outputs it in a formatted way.
JSON.stringify({ foo: "sample", bar: "sample" }, null, 4)This turns
{ "foo": "sample", "bar": "sample" }into
{ "foo": "sample", "bar": "sample" }Now the data is a readable format you can use the Google Code Prettify script as suggested by @A. Levy to colour code it.
It is worth adding that IE7 and older browsers do not support the JSON.stringify method.
更多推荐
将JSON显示为HTML
发布评论