| 范文 | 1
 {"name": "monkey","age": "24","height": 164.0}
 如果想让以上json字符串在页面上展示的比较易读,即变成下面的style:
 1
 {
 2
 "name": "monkey",
 3
 "age": "24",
 4
 "height": 164.0cm
 5
 }
 本文介绍的方法基于javascript ,代码如下:
 01
 <html>
 02
 <head>/
 03
 //style中是css代码
 04
 <style type="text/css">
 05
 body
 06
 {
 07
 white-space: pre;
 08
 font-family: monospace;
 09
 }
 10
 </style>
 11
 //script中是javascript代码
 12
 <script>
 13
 window.error_id_msgs = <%= error_id_msgs | raw %>;
 14
 function myFunction() {
 15
 document.body.innerHTML = "";
 16
 document.body.appendChild(document.createTextNode(JSON.stringify(window.error_id_msgs, null, 4)));
 17
 }
 18
 </script>
 19
 </head>
 20
 <body onload="myFunction()">//表示页面加载时调用myFunction()
 21
 </body>
 22
 </html>
 其中window.error_id_msgs是所要转换的json对象,css和myFunction结合可实现页面展示转换
 |