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
Table of contents 1. Introduction 2. Code Impleme...
This article uses an example to describe the simp...
Table of contents 1. Installation 2. Import in ma...
border-radius:10px; /* All corners are rounded wi...
Table of contents The principle of Vue asynchrono...
Preface The sleep system function in MySQL has fe...
<br />The following are the problems I encou...
1. Background Although I have read many blogs or ...
The previous article introduced the implementatio...
Reference: Docker official redis documentation 1....
Table of contents Component Communication Introdu...
Table of contents Implementing HTML Add CSS Imple...
You can often see articles about CSS drawing, suc...
Table of contents Problem Description Historical ...
Table of contents 1. Four concepts 1. JavaScript ...