将JSON显示为HTML

编程入门 行业动态 更新时间:2024-10-28 12:17:27
本文介绍了将JSON显示为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

关于如何将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

本文发布于:2023-11-28 17:25:53,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JSON   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!