Home Web Front-end JS Tutorial JSON.stringify() usage introduction

JSON.stringify() usage introduction

Aug 18, 2017 am 10:55 AM
javascript usage

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 }"
Copy after login

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
};
Copy after login

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":{}}}
Copy after login

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};
Copy after login

Error message:


ReferenceError: foo is not defined
 at repl:1:14
Copy after login

In 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);
// {}
Copy after login

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']"
Copy after login

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"}"
Copy after login

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"}"
Copy after login

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)
Copy after login

3. Optional parameters The complete definition of JSON.stringify is as follows:

JSON.stringify(value, replacer?, space?)
Copy after login

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. Array

Only the attributes in the array are stringify:

##
let foo = {
 a : 1,
 b : "string",
 c : false
};
JSON.stringify(foo, ['a', 'b']);
//"{"a":1,"b":"string"}"
Copy after login

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}}"
Copy after login

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}}"
Copy after login

通过改写上面的函数加入适当的输出,可以看到具体的执行步骤:


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
Copy after login

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}
// }"
Copy after login

一道三颗星的思考题:为什么打印结果的倒数第三行有两个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!

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Analyze the usage and classification of JSP comments Analyze the usage and classification of JSP comments Feb 01, 2024 am 08:01 AM

Classification and Usage Analysis of JSP Comments JSP comments are divided into two types: single-line comments: ending with, only a single line of code can be commented. Multi-line comments: starting with /* and ending with */, you can comment multiple lines of code. Single-line comment example Multi-line comment example/**This is a multi-line comment*Can comment on multiple lines of code*/Usage of JSP comments JSP comments can be used to comment JSP code to make it easier to read

How to correctly use the exit function in C language How to correctly use the exit function in C language Feb 18, 2024 pm 03:40 PM

How to use the exit function in C language requires specific code examples. In C language, we often need to terminate the execution of the program early in the program, or exit the program under certain conditions. C language provides the exit() function to implement this function. This article will introduce the usage of exit() function and provide corresponding code examples. The exit() function is a standard library function in C language and is included in the header file. Its function is to terminate the execution of the program, and can take an integer

Usage of WPSdatedif function Usage of WPSdatedif function Feb 20, 2024 pm 10:27 PM

WPS is a commonly used office software suite, and the WPS table function is widely used for data processing and calculations. In the WPS table, there is a very useful function, the DATEDIF function, which is used to calculate the time difference between two dates. The DATEDIF function is the abbreviation of the English word DateDifference. Its syntax is as follows: DATEDIF(start_date,end_date,unit) where start_date represents the starting date.

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Detailed explanation and usage introduction of MySQL ISNULL function Detailed explanation and usage introduction of MySQL ISNULL function Mar 01, 2024 pm 05:24 PM

The ISNULL() function in MySQL is a function used to determine whether a specified expression or column is NULL. It returns a Boolean value, 1 if the expression is NULL, 0 otherwise. The ISNULL() function can be used in the SELECT statement or for conditional judgment in the WHERE clause. 1. The basic syntax of the ISNULL() function: ISNULL(expression) where expression is the expression to determine whether it is NULL or

How to use Apple shortcuts How to use Apple shortcuts Feb 18, 2024 pm 05:22 PM

How to use Apple shortcut commands With the continuous development of technology, mobile phones have become an indispensable part of people's lives. Among many mobile phone brands, Apple mobile phones have always been loved by users for their stable systems and powerful functions. Among them, the Apple shortcut command function makes users’ mobile phone experience more convenient and efficient. Apple Shortcuts is a feature launched by Apple for iOS12 and later versions. It helps users simplify their mobile phone operations by creating and executing custom commands to achieve more efficient work and

Using the distinct keyword in parsing SQL Using the distinct keyword in parsing SQL Feb 18, 2024 pm 09:21 PM

Detailed explanation of distinct usage in SQL In SQL databases, we often encounter situations where we need to remove duplicate data. At this time, we can use the distinct keyword, which can help us remove duplicate data and make the query results clearer and more accurate. The basic usage of distinct is very simple, just use the distinct keyword in the select statement. For example, the following is a normal select statement: SELECTcolumn_name

Use CSS Transform to transform elements Use CSS Transform to transform elements Feb 24, 2024 am 10:09 AM

Usage of Transform in CSS The Transform property of CSS is a very powerful tool that can perform operations such as translation, rotation, scaling and tilting of HTML elements. It can dramatically change the appearance of elements and make web pages more creative and dynamic. In this article, we will introduce the various uses of Transform in detail and provide specific code examples. 1. Translate (Translate) Translate refers to moving an element a specified distance along the x-axis and y-axis. Its syntax is as follows: tran

See all articles