To display the JSON data in a beautiful indented format, you can use the simplest JSON.stringify function, because this function has two uncommon parameters at the end. See the description on MDN https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify. The sample code is as follows: <html> <head> <meta charset="utf-8" /> <title>hello</title> <style> pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; } .string { color: green; } .number { color: darkorange; } .boolean { color: blue; } .null { color: magenta; } .key { color: red; } </style> <script type="text/javascript"> function syntaxHighlight(json) { if (typeof json != 'string') { json = JSON.stringify(json, undefined, 2); } json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) { var cls = 'number'; if (/^"/.test(match)) { if (/:$/.test(match)) { cls = 'key'; } else { cls = 'string'; } } else if (/true|false/.test(match)) { cls = 'boolean'; } else if (/null/.test(match)) { cls = 'null'; } return '<span class="' + cls + '">' + match + '</span>'; }); } </script> </head> <body> <pre id="result"> </pre> <script type="text/javascript"> var songResJson = { "service": "ALL", "qt": 581, "content": { "answer": { "song": "If fate only lasts until we meet", "album": "If fate only lasts until we meet", "artist": "Wu Qilong Yan Yidan", "pic_url": "upload/2022/web/5921969627395387.jpg" }, "scene": "music" } } document.getElementById('result').innerHTML = syntaxHighlight(songResJson); // $('#result').html(syntaxHighlight(songResJson)); </script> </body> </html> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. |
<<: About Zabbix custom monitoring items and triggers
>>: Sample code for implementing neon button animation effects with CSS3.0
The specific code for JavaScript to implement the...
“How to make a website look high-end? Or more des...
Install the latest stable version of MySQL on Lin...
1. First download from the official website of My...
Classification of CSS styles 1. Internal style --...
1. HTML Overview 1.HTML: Hypertext Markup Languag...
Preface In backend development, in order to preve...
1. CSS file naming conventions Suggestion: Use le...
How to uninstall Mysql perfectly? Follow the step...
I encountered a case at work that I had never wri...
1. Delete folders Example: rm -rf /usr/java The /...
environment: 1. Windows Server 2016 Datacenter 64...
Aggregate functions Acts on a set of data and ret...
1. Download the required kernel version 2. Upload...
Table of contents Layout part: <div id="a...