How to display JSON data in HTML

How to display JSON data in HTML

background:

Sometimes we need to display json data directly on the page (for example, when doing an interface testing project, we need to display the results returned by the interface directly), but if the string is displayed directly, it is not convenient to view. Needs formatting.

Solution:

In fact, JSON.stringify itself can format JSON. The specific usage is:

JSON.stringify(res, null, 2); //res is the object to be JSONified, 2 is spacing

If you want a better effect, you need to add formatting code and style:

js code:

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>';
    });
}

Style code:

<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>

HTML code:

<pre id="result">
</pre>

Calling code:

$('#result').html(syntaxHighlight(res));

Effect:

The above is the method I introduced to you to display JSON data in HTML. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

<<:  Solution to occasional crash of positioning background service on Linux

>>:  Let's talk briefly about the changes in setup in vue3.0 sfc

Recommend

Vue implements sending emoticons in chat box

The specific code for sending emoticons in the vu...

JavaScript Factory Pattern Explained

Table of contents Simple Factory Factory Method S...

Basic tutorial on controlling Turtlebot3 mobile robot with ROS

Chinese Tutorial https://www.ncnynl.com/category/...

What to do after installing Ubuntu 20.04 (beginner's guide)

Ubuntu 20.04 has been released, bringing many new...

Getting Started Tutorial for Beginners ⑨: How to Build a Portal Website

Moreover, an article website built with a blog pro...

Methods for deploying MySQL services in Docker and the pitfalls encountered

I have been learning porters recently. I feel lik...

Perfect solution for vertical centering of form elements

Copy code The code is as follows: <!DOCTYPE ht...

MySQL transaction analysis

Transaction A transaction is a basic unit of busi...

Vue based on Element button permission implementation solution

Background requirements: The ERP system needs to ...

Use semantic tags to write your HTML compatible with IE6,7,8

HTML5 adds more semantic tags, such as header, fo...

Detailed steps for setting up host Nginx + Docker WordPress Mysql

environment Linux 3.10.0-693.el7.x86_64 Docker ve...

js to implement collision detection

This article example shares the specific code of ...