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

Vue data two-way binding implementation method

Table of contents 1. Introduction 2. Code Impleme...

MySQL derived table (Derived Table) simple usage example analysis

This article uses an example to describe the simp...

Detailed explanation of how to use element-plus in Vue3

Table of contents 1. Installation 2. Import in ma...

border-radius method to add rounded borders to elements

border-radius:10px; /* All corners are rounded wi...

A brief analysis of Vue's asynchronous update of DOM

Table of contents The principle of Vue asynchrono...

Detailed explanation of special phenomena examples of sleep function in MySQL

Preface The sleep system function in MySQL has fe...

Problems and experiences encountered in web development

<br />The following are the problems I encou...

A brief discussion on the correct posture of Tomcat memory configuration

1. Background Although I have read many blogs or ...

Promise encapsulation wx.request method

The previous article introduced the implementatio...

Docker installs the official Redis image and enables password authentication

Reference: Docker official redis documentation 1....

Detailed explanation of React component communication

Table of contents Component Communication Introdu...

HTML+CSS+JavaScript to create a simple tic-tac-toe game

Table of contents Implementing HTML Add CSS Imple...

Implementation of Single Div drawing techniques in CSS

You can often see articles about CSS drawing, suc...

Summary of MySQL 8.0 Online DDL Quick Column Addition

Table of contents Problem Description Historical ...

Javascript operation mechanism Event Loop

Table of contents 1. Four concepts 1. JavaScript ...