How to display and format json data on html page

How to display and format json data on html page

JSON data is displayed and formatted on the HTML page

1. Display effect diagram

Description:

  • All key values ​​are marked in red, indicating important parameters;
  • Values ​​are marked in different colors: numeric values ​​are in orange, strings are in green, and Booleans are in blue. . .

2. Source code display

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <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; }

    .showinfo{
        position: absolute;
        background-color: #eef1f8;
        width: 200px;
        padding: 5px;
        border-radius: 4px;
        border: 1px solid #ccc;
        display: none;
    }
    .showinfo pre{
        padding: 5px;
        border: 1px solid #ccc;
        margin:0;
    }
    table,th,td{
        border:1px solid blue;
    }
</style>
<script src="./js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">

    $(document).ready(function(){
        $(".show-rough").mouseover(function(){
            var left = $(this).offset().left + $(this).width() +20; //Calculate div display position var top = $(this).offset().top + 20;
            var _jsonDate = $.parseJSON($(this).text());
            var showJson = syntaxHighlight(_jsonDate);
            $("#show-info").css({"left":left,"top":top}).show();
            $("#show-pre").html(showJson);
        });
        $(".show-rough").mouseout(function(){
            $("#show-info").hide().html();
            $("#show-pre").html();
        })
    });
    //Process json data and use regular expressions to filter out parameters of different types 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>
<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>json data</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>小三</td>
            <td class="show-rough">{ "name": "小三", "address":"No. 23 Beijing Road","age":16, "email": "[email protected]","Object":[{"Position":"Manager"}],"del":[] }</td>
        </tr>
        <tr>
            <td>小四</td>
            <td class="show-rough">{ "name": "Xiao Si", "address":"No. 01 Shanghai Road","age":27, "email": "[email protected]","Object":[],"del":[] }</td>
        </tr>
    </tbody>
</table>
<div id="show-info" class="showinfo">
    <pre id="show-pre">

</pre>
</div>
</body>
</html>

3. Source code upload

Source code download address

This is the end of this article about how to display JSON data and format it on HTML pages. For more relevant content about how to display JSON and format it on HTML pages, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Node script realizes automatic sign-in and lottery function

>>:  A brief discussion on browser compatibility issues in JavaScript

Recommend

The most detailed method to install docker on CentOS 8

Install Docker on CentOS 8 Official documentation...

Detailed explanation of the use of DockerHub image repository

Previously, the images we used were all pulled fr...

CSS3 click button circular progress tick effect implementation code

Table of contents 8. CSS3 click button circular p...

Multiple methods to modify MySQL root password (recommended)

Method 1: Use the SET PASSWORD command MySQL -u r...

MySQL 8.0.20 installation and configuration tutorial under Docker

Docker installs MySQL version 8.0.20 for your ref...

Docker Basics

Preface: Docker is an open source application con...

Nginx load balancing algorithm and failover analysis

Overview Nginx load balancing provides upstream s...

Summary of Linux file directory management commands

touch Command It has two functions: one is to upd...

Vue resets data to its initial state

In some cases, the data in data needs to be reuse...

Detailed steps for completely uninstalling MySQL 5.7

This article mainly summarizes various problems o...

Implementing image fragmentation loading function based on HTML code

Today we will implement a fragmented image loadin...

mysql 5.7.11 winx64 initial password change

Download the compressed version of MySQL-5.7.11-w...

Detailed explanation of how to gracefully delete a large table in MySQL

Preface To delete a table, the command that comes...