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

Add a startup method to Linux (service/script)

Configuration file that needs to be loaded when t...

Problem analysis of using idea to build springboot initializer server

Problem Description Recently, when I was building...

JavaScript to implement image preloading and lazy loading

This article shares the specific code for impleme...

Introduction to the process of creating TCP connection in Linux system

Table of contents Steps to create TCP in Linux Se...

Summary of constructor and super knowledge points in react components

1. Some tips on classes declared with class in re...

How to add color mask to background image in CSS3

Some time ago, during development, I encountered ...

A brief discussion on MySQL temporary tables and derived tables

About derived tables When the main query contains...

Several principles for website product design reference

The following analysis is about product design pr...

Example code for implementing hexagonal borders with CSS3

The outermost boxF rotates 120 degrees, the secon...

Perfect solution to the problem of webpack packaging css background image path

Inside the style tag of the vue component, there ...

Detailed explanation of JavaScript upload file limit parameter case

Project scenario: 1. Upload file restrictions Fun...