JSON.stringify(value[, replacer [, space]]) 方法将一个 JavaScript 对象或值转换为 JSON 字符串,如果指定了一个 replacer 函数,则可以选择性地替换值,或者指定的 replacer 是数组,则可选择性地仅包含数组指定的属性。
Note 1
在 JSON.stringify 方法对对象进行序列化时,如果对象的属性是 undefined/Symbol 值或任意函数时,不会对其进行处理。
这会导致被序列化的对象中的属性并不会按照原定的顺序被输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| const someObj = { _id: 'id', productCode: 'P28120068', productDetail: { name: 'Star Insure', price: 1200, amount: 1000000 }, created_at: 'Fri Nov 13 2020 09:25:31 GMT+0800 (China Standard Time)', updated_at: 'Fri Nov 13 2020 09:25:31 GMT+0800 (China Standard Time)', nullValue: null, undefinedValue: undefined, symbolValue: Symbol('TypeScript'), arrowLog: () => console.log(this), funLog: function() { console.log(this) }, }
console.log(JSON.stringify(someObj));
|
Note 2
在 JSON.stringify 方法对数组进行序列化时,如果数组元素的值是 undefined、Symbol 值或任意函数时,会被序列化为 null。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| const someArr = [ null, undefined, Symbol('TypeScript'), () => console.log(this), function() { console.log(this) } ]
console.table(JSON.stringify(someArr));
|
Note 3
如果 undefined、Symbol 值或任意函数作为单独的值被 JSON.stringify 序列化为 undefined。
1 2 3 4 5 6
|
console.log(JSON.stringify(undefined)); console.log(JSON.stringify(Symbol('TypeScript'))); console.log(JSON.stringify(() => console.log(this)));
|
Note 4
如果目标值有 toJSON 方法,则 toJSON 来决定 JSON.stringify 返回的值。
1 2 3 4 5
| console.log(JSON.stringify({ name: 'Jimmy', age: 20, toJSON: () => 'this is a toJSON function', }));
|
Note 5
NaN, InInfinity 及 null 会被JSON.stringify 序列化为 null。
1 2 3 4 5 6 7 8 9
|
console.log(NaN); console.log(JSON.stringify(NaN)); console.log(Infinity); console.log(JSON.stringify(Infinity)); console.log(null); console.log(JSON.stringify(null));
|
Note 6
数字、字符串和布尔值的包装值在被 JSON.stringify 序列化时会解包成对应的基础值。
1 2 3
| const packArr = [ new Number(1), new String('TypeScript'), new Boolean(false)]; console.log(packArr); console.log(JSON.stringify(packArr));
|
Note 7
JSON.stringify 在序列化对象时,仅会序列化可枚举的属。
1 2 3 4 5
| const enumerableObj = Object.create(null, { name: { value: 'jack', enumerable: true }, age: { value: 20, enumerable: false } }); console.log(JSON.stringify(enumerableObj));
|
Note 8
如果 JSON.stringify 的目标对象发生循环引用时,会报错。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
const objA = { name: 'Object A', refB: null, };
const objB = { name: 'Object B', refA: objA, };
objA.refB = objB;
|
Note 9
以 Symbol 值为键的属性值,在被 JSON.stringify 序列化时会被忽略。
1 2 3 4 5 6 7 8 9
|
const symbolObj = { name: 'Symbol', [Symbol.for('Age')]: 20, }; console.log(symbolObj); console.log(JSON.stringify(symbolObj));
|
Note 10
当 JSON.stringify 的第二个参数是函数时, 可单独对某个值进行处理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| const someObj = { _id: 'id', productCode: 'P28120068', productDetail: { name: 'Star Insure', price: 1200, amount: 1000000 }, created_at: 'Fri Nov 13 2020 09:25:31 GMT+0800 (China Standard Time)', updated_at: 'Fri Nov 13 2020 09:25:31 GMT+0800 (China Standard Time)', nullValue: null, undefinedValue: undefined, symbolValue: Symbol('TypeScript'), arrowLog: () => console.log(this), funLog: function() { console.log(this) }, }
console.log(JSON.stringify(someObj, (k, v) => { console.log(v) if (typeof v === 'undefined') return 'undefined'; return JSON.stringify(v); }));
|
当 JSON.stringify 的第二个参数是数组时,可在数组中加入属性名,来规定返回哪些属性的序列化值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| const someObj = { _id: 'id', productCode: 'P28120068', productDetail: { name: 'Star Insure', price: 1200, amount: 1000000 }, created_at: 'Fri Nov 13 2020 09:25:31 GMT+0800 (China Standard Time)', updated_at: 'Fri Nov 13 2020 09:25:31 GMT+0800 (China Standard Time)', nullValue: null, undefinedValue: undefined, symbolValue: Symbol('TypeScript'), arrowLog: () => console.log(this), funLog: function() { console.log(this) }, }
console.log(JSON.stringify(someObj, ['productCode', 'arrowLog']));
|