将JSON显示为HTML [英] Display JSON as HTML

查看:198
本文介绍了将JSON显示为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何将JSON嵌入到HTML页面中并使用JSON格式化为人类可读样式的建议?例如,当您在浏览器中查看XML时,大多数浏览器都会显示XML格式(缩进,适当的换行符等)。对于JSON,我希望获得相同的最终结果。



颜色语法突出显示将是一项奖励。



谢谢

解决方案

您可以使用带有未格式化JSON的JSON.stringify函数。它以格式化的方式输出它。

  JSON.stringify({foo:sample,bar:sample},null,4)

$ b

 {foo:sample,bar:sample} 

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆