search
HomeWeb Front-endJS TutorialDetailed explanation of how to realize json data visualization

前言

本文介绍的是如何实现json数据可视化,要用到的核心是JSON.stringify这个函数,没想到吧,平时我们只把它用来序列号json数据。

JSON.stringify 函数

将 JavaScript 值转换为 JavaScript 对象表示法 (Json) 字符串。

语法

JSON.stringify(value [, replacer] [, space])

   

参数

value

     必需。  要转换的 JavaScript 值(通常为对象或数组)。  

replacer

     可选。  用于转换结果的函数或数组。  

     如果 replacer 为函数,则 JSON.stringify 将调用该函数,并传入每个成员的键和值。  使用返回值而不是原始值。  如果此函数返回 undefined,则排除成员。  根对象的键是一个空字符串:""。  

     如果 replacer 是一个数组,则仅转换该数组中具有键值的成员。  成员的转换顺序与键在数组中的顺序一样。  当 value 参数也为数组时,将忽略 replacer 数组。  

space

     可选。  向返回值 JSON 文本添加缩进、空格和换行符以使其更易于读取。  

     如果省略 space,则将生成返回值文本,而没有任何额外空格。

     如果 space 是一个数字,则返回值文本在每个级别缩进指定数目的空格。  如果 space 大于 10,则文本缩进 10 个空格。  

     如果 space 是一个非空字符串(例如“\t”),则返回值文本在每个级别中缩进字符串中的字符。

     如果 space 是长度大于 10 个字符的字符串,则使用前 10 个字符。

返回值

一个包含 JSON 文本的字符串。

json数据可视化

我们要用到的就是这第三个参数,它可以指定在生成的字符串中加多少空格,从而生成有一定格式的字符串。生成的字符串我们可以放在

标签中,这样就能很好的显示缩进。然后呢,为了让生成的数据有高亮效果,我们还可以写一个简单的高亮函数。<p>基本就是这么个原理啦,请看代码实现:</p><pre class='brush:php;toolbar:false;'>function output(inp) {
 document.body.appendChild(document.createElement(&#39;pre&#39;)).innerHTML = inp;
}
 
function syntaxHighlight(json) {
 json = json.replace(/&/g, &#39;&&#39;).replace(/</g, &#39;<&#39;).replace(/>/g, &#39;>&#39;);
 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 = &#39;number&#39;;
  if (/^"/.test(match)) {
   if (/:$/.test(match)) {
    cls = &#39;key&#39;;
   } else {
    cls = &#39;string&#39;;
   }
  } else if (/true|false/.test(match)) {
   cls = &#39;boolean&#39;;
  } else if (/null/.test(match)) {
   cls = &#39;null&#39;;
  }
  return &#39;<span class="&#39; + cls + &#39;">&#39; + match + &#39;</span>&#39;;
 });
}
 
var obj = {
 num: 1234,
 str: &#39;字符串&#39;,
 arr: [1,2,3,4,5,6],
 obj: {
  name: &#39;tom&#39;,
  age: 10,
  like: [&#39;a&#39;, &#39;b&#39;]
 }
};
var str = JSON.stringify(obj, undefined, 4);

 

output(syntaxHighlight(str));

   

最终生成的效果就是这样的:

Detailed explanation of how to realize json data visualization

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function