Veteran drivers, do you know that JSON.stringify also has the second and third optional parameters? What are they? Doesn’t it feel incredible? The following article will introduce you to some related information about JSON.stringify that you may not know. The article introduces it in detail through sample code. Friends who need it can refer to it.
Preface
JSON has gradually replaced XML and is widely used by developers around the world. This article explains in depth some of the details of using JSON.stringify in JavaScript. First, a brief review of JSON and JavaScript:
Not all legal JSON is valid JavaScript;
JSON is just a text format;
Numbers in JSON are decimal.
1. JSON.stringify
let foo = { a: 2, b: function() {} }; JSON.stringify(foo); // "{ "a": 2 }"
The JSON.stringify function will Convert a JavaScript object into textual JSON. Properties that cannot be textualized are ignored. The value of attribute b in foo is the function definition and is not converted and lost.
What other attributes cannot be converted?
1. Circular reference
If the attribute value of an object points back to the object itself in some indirect way, then it is a circular reference. For example:
var bar = { a: { c: foo } }; var foo = { b: bar };
The attribute c points to itself. If it is parsed layer by layer, it will enter an infinite loop. Let's try to print it out and see:
let fooStringified = JSON.stringify(foo); console.log(fooStringified); // {"b":{"a":{}}}
The attribute of c points to the foo object, and the b attribute in the foo object points to the bar object and cannot be processed. The whole thing is ignored. Returns an empty object.
The following definition (example in the original text) cannot be compiled:
##
let foo = {b : foo};Error message:
ReferenceError: foo is not defined at repl:1:14In the functional language Haskell, because of the Lazy Evaluation technology, a similar definition method can be used.
2. Symbol and undefined
##
let foo = { b: undefined }; JSON.stringify(foo); // {} // Symbols foo.b = Symbol(); JSON.stringify(foo); // {}
Exceptions
in the array , elements that cannot be stringified are filled with null.
let foo = [Symbol(), undefined, function() {}, 'works'] JSON.stringify(foo); // "[null,null,null,'works']"
This can maintain the "shape" of the array itself, that is, the original index of each element. Why can’t some attributes be stringified?
Because JSON is a universal text format and has nothing to do with language. Imagine that if the function definition is also stringify, how to determine which language it is and present it in an appropriate way will become particularly complicated. Especially some features related to language, such as Symbol in JavaScript.
ECMASCript officials also specifically emphasized this point:
It does not attempt to impose ECMAScript's internal data representations on other programming languages. Instead, it shares a small subset of ECMAScript's textual representations with all other programming languages.
2. Rewrite the object toJSON function#A way to bypass the inability to stringify certain properties of an object is to implement the object's toJSON Method to customize the object being stringified. Since almost every AJAX call uses JSON.stringify, mastering this technique will be of great help in handling server interactions.
Similar to toString, which allows you to return elements in an object in the form of a string, toJSON provides a method to convert attributes in the object that cannot be stringify, so that the next JSON call is made. stringify can convert it into JSON format.
function Person (first, last) { this.firstName = first; this.last = last; } Person.prototype.process = function () { return this.firstName + ' ' + this.lastName; }; let ade = new Person('Ade', 'P'); JSON.stringify(ade); // "{"firstName":"Ade","last":"P"}"
The process function of Person instance ade is not stringify. Imagine that if the server only wants the full name of ade, instead of getting the last name and first name respectively, we can directly define toJSON to achieve the goal:
Person.prototype.toJSON = function () { return { fullName: this.process(); }; }; let ade = new Person('Ade', 'P'); JSON.stringify(ade); // "{"fullName":"Ade P"}"
The advantage of defining toJSON is reuse For flexibility and stability, you can use ade with any library, and the data transferred will be the fullName returned by your toJSON definition.
// jQuery $.post('endpoint', ade); // Angular 2 this.httpService.post('endpoint', ade)
3. Optional parameters The complete definition of JSON.stringify is as follows:
JSON.stringify(value, replacer?, space?)
replacer and space are optional parameters, let’s explain them separately next. Replacer
replacer is a filter function or an array containing the attribute names to be stringified. If not defined, all properties are stringified by default.
1. ArrayOnly the attributes in the array are stringify:
##let foo = { a : 1, b : "string", c : false }; JSON.stringify(foo, ['a', 'b']); //"{"a":1,"b":"string"}"
Nested attributes are also It will also be filtered:
let bar = { a : 1, b : { c : 2 } }; JSON.stringify(bar, ['a', 'b']); //"{"a":1,"b":{}}" JSON.stringify(bar, ['a', 'b', 'c']); //"{"a":1,"b":{"c":2}}"
Defining the filter array sometimes does not meet the needs, then you can customize the filter function.
2. Function
The filter function takes each attribute and value in the object as input, and the return value has the following situations:
Returning undefined means ignoring the attribute;- Returns a string, Boolean value or number will be stringify;
- The returned object will trigger a recursive call until a property of the basic type is encountered;
返回无法stringify的值将会被忽略;
let baz = { a : 1, b : { c : 2 } }; // 返回大于1的值 let replacer = function (key, value) { if(typeof === 'number') { return value > 1 ? value: undefined; } return value; }; JSON.stringify(baz, replacer); // "{"b":{"c":2}}"
通过改写上面的函数加入适当的输出,可以看到具体的执行步骤:
let obj = { a : 1, b : { c : 2 } }; let tracer = function (key, value){ console.log('Key: ', key); console.log('Value: ', value); return value; }; JSON.stringify(obj, tracer); // Key: // Value: Object {a: 1, b: Object} // Key: a // Value: 1 // Key: b // Value: Object {c: 2} // Key: c // Value: 2
space
你是否意识到调用默认的JSON.stringify返回的值只要一行,而且完全没有空格?如果想要更加美观的打印出来,那么就需要使用space这个参数了。
我告诉你一个非常简单的方法:通过tab(‘\t')来分割即可。
let space = { a : 1, b : { c : 2 } }; // 使用制表符 JSON.stringify(space, undefined, '\t'); // "{ // "a": 1, // "b": { // "c": 2 // } // }" JSON.stringify(space, undefined, ''); // {"a":1,"b":{"c":2}} // 自定义分隔符 JSON.stringify(space, undefined, 'a'); // "{ // a"a": 1, // a"b": { // aa"c": 2 // a} // }"
一道三颗星的思考题:为什么打印结果的倒数第三行有两个a呢?
结论
本文介绍了一些使用toJSON的技巧:
无法stringify的几种类型
使用toJSON来自定义JSON.stringify的属性
可选参数replacer的两种定义方法来过滤属性
可选参数space用来格式化输出结果
数组和对象中如果包含无法stringify的元素的时候的区别
The above is the detailed content of JSON.stringify() usage introduction. For more information, please follow other related articles on the PHP Chinese website!

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software