Example of JSON output in HTML format (test interface)

Example of JSON output in HTML format (test interface)

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

Recommend

Native JavaScript implementation of progress bar

The specific code for JavaScript to implement the...

How to make a website look taller and more designed

“How to make a website look high-end? Or more des...

Installation tutorial of the latest stable version of MySQL 5.7.17 under Linux

Install the latest stable version of MySQL on Lin...

MySql 5.7.20 installation and configuration of data and my.ini files

1. First download from the official website of My...

Introduction to CSS style classification (basic knowledge)

Classification of CSS styles 1. Internal style --...

Detailed explanation of HTML basic tags and structures

1. HTML Overview 1.HTML: Hypertext Markup Languag...

How to quickly paginate MySQL data volumes of tens of millions

Preface In backend development, in order to preve...

Detailed explanation of DIV+CSS naming rules can help achieve SEO optimization

1. CSS file naming conventions Suggestion: Use le...

How to uninstall MySQL cleanly (tested and effective)

How to uninstall Mysql perfectly? Follow the step...

How to get the dynamic number of remaining words in textarea

I encountered a case at work that I had never wri...

How to delete folders, files, and decompress commands on Linux servers

1. Delete folders Example: rm -rf /usr/java The /...

Django online deployment method of Apache

environment: 1. Windows Server 2016 Datacenter 64...

MySQL uses aggregate functions to query a single table

Aggregate functions Acts on a set of data and ret...

How to compile the Linux kernel

1. Download the required kernel version 2. Upload...

Vue makes a simple random roll call

Table of contents Layout part: <div id="a...