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

A brief analysis of crontab task scheduling in Linux

1. Create a scheduling task instruction crontab -...

Docker starts MySQL configuration implementation process

Table of contents Actual combat process Let's...

Detailed explanation of the top ten commonly used string functions in MySQL

Hello everyone! I am Mr. Tony who only talks abou...

Analyzing the MySql CURRENT_TIMESTAMP function by example

When creating a time field DEFAULT CURRENT_TIMEST...

How to query date and time in mysql

Preface: In project development, some business ta...

Analysis of pitfalls in rounding operation of ROUND function in MySQL

This article uses examples to illustrate the pitf...

CSS method of clearing float and BFC

BFC BFC: Block Formatting Context BFC layout rule...

Summary of relevant knowledge points of ajax in jQuery

Preface Students who learn JavaScript know that A...

Complete steps to quickly build a vue3.0 project

Table of contents 1. We must ensure that the vue/...

MySQL data operation-use of DML statements

illustrate DML (Data Manipulation Language) refer...

How to implement gzip compression in nginx to improve website speed

Table of contents Why use gzip compression? nginx...

CSS3 to achieve menu hover effect

Result: html <nav id="nav-1"> <...