JSON.stringify()
方法将一个 JavaScript 对象或值转换为 JSON 字符串,如果指定了一个 replacer
函数,则可以选择性地替换值,或者指定的 replacer
是数组,则可选择性地仅包含数组指定的属性。
语法
JSON.stringify(value[, replacer [, space]])
参数
value 将要序列化成 一个 JSON 字符串的值。
replacer | 可选 如果该参数是一个函数,则在序列化过程中,被序列化的值的每个属性都会经过该函数的转换和处理;如果该参数是一个数组,则只有包含在这个数组中的属性名才会被序列化到最终的 JSON 字符串中;如果该参数为 null 或者未提供,则对象所有的属性都会被序列化。
space | 可选 指定缩进用的空白字符串,用于美化输出(pretty-print);如果参数是个数字,它代表有多少的空格;上限为10。该值若小于1,则意味着没有空格;如果该参数为字符串(当字符串长度超过10个字母,取其前10个字母),该字符串将被作为空格;如果该参数没有提供(或者为 null),将没有空格。
返回值 一个表示给定值的JSON字符串。
特性 对于 undefined、任意的函数以及 symbol 三个特殊的值分别作为对象属性的值、数组元素、单独的值时的不同返回结果。
undefined、任意的函数以及 symbol 作为对象属性值时 JSON.stringify() 跳过(忽略)对它们进行序列化示例代码 1 2 3 4 5 6 7 8 9 10 11 const data = { a: "aaa" , b: undefined , c: Symbol ("dd" ), fn: function ( ) { return true ; } }; JSON .stringify(data);
undefined、任意的函数以及 symbol 作为数组元素值时,JSON.stringify() 将会将它们序列化为 null示例代码 1 2 3 4 5 JSON .stringify(["aaa" , undefined , function aa ( ) { return true }, Symbol ('dd' )])
undefined、任意的函数以及 symbol 被 JSON.stringify() 作为单独的值进行序列化时都会返回 undefined示例代码 1 2 3 4 5 6 JSON .stringify(function a ( ) {console .log('a' )})JSON .stringify(undefined )JSON .stringify(Symbol ('dd' ))
非数组对象的属性不能保证以特定的顺序出现在序列化后的字符串中 因为null
function
symbol
都会被忽略掉,所以必然是没有特定的顺序的
示例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 const data = { a: "aaa" , b: undefined , c: Symbol ("dd" ), fn: function ( ) { return true ; }, d: "ddd" }; JSON .stringify(data); JSON .stringify(["aaa" , undefined , function aa ( ) { return true }, Symbol ('dd' ),"eee" ])
转换值如果有 toJSON() 函数,该函数返回什么值,序列化结果就是什么值,并且忽略其他属性的值。 拥有toJSON
函数后进行序列化会默认走toJSON
示例代码 1 2 3 4 5 6 7 JSON .stringify({ say: "hello JSON.stringify" , toJSON: function ( ) { return "today i learn" ; } })
JSON.stringify() 将会正常序列化 Date 的值 默认调用的是toISOString()
会自动转化为国际化时间
示例代码 1 2 JSON .stringify({ now : new Date () });
NaN 和 Infinity 格式的数值及 null 都会被当做 null。 示例代码 1 2 3 4 5 6 JSON .stringify(NaN )JSON .stringify(null )JSON .stringify(Infinity )
布尔值、数字、字符串的包装对象在序列化过程中会自动转换成对应的原始值。 示例代码 1 2 JSON .stringify([new Number (1 ), new String ("false" ), new Boolean (false )]);
其他类型的对象,包括 Map/Set/WeakMap/WeakSet,仅会序列化可枚举的属性。 仅会序列化可枚举的属性
示例代码 1 2 3 4 5 6 7 8 9 10 11 JSON .stringify( Object .create( null , { x: { value : 'json' , enumerable : false }, y: { value : 'stringify' , enumerable : true } } ) );
对包含循环引用的对象(对象之间相互引用,形成无限循环)执行此方法,会抛出错误。 示例代码 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 obj = { name: "loopObj" }; const loopObj = { obj }; obj.loopObj = loopObj; function deepClone (obj ) { return JSON .parse(JSON .stringify(obj)); } deepClone(obj)
所有以 symbol 为属性键的属性都会被完全忽略掉,即便 replacer 参数中强制指定包含了它们。 示例代码 1 2 3 4 5 6 7 JSON .stringify({ [Symbol .for("json" )]: "stringify" }, function (k, v ) { if (typeof k === "symbol" ) { return v; } })
如何序列化undefined
function
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 const data = { a: "aaa" , b: undefined , c: Symbol ("dd" ), fn: function ( ) { return true ; } }; JSON .stringify(data); JSON .stringify(data, (key, value ) => { switch (typeof value) { case "undefined" : return "undefined" ; case "symbol" : return value.toString(); case "function" : return value.toString(); } return value; })
或者
示例代码 1 2 3 4 5 6 7 8 9 10 11 JSON .stringify(data, (key, value ) => { switch (typeof value) { case "undefined" : return "undefined" ; case "symbol" : return value.toString(); case "function" : return value.toString(); } return value; })
TO
示例代码 1 JSON .stringify(data, (key, value ) => (['undefined' , 'symbol' , 'function' ].includes(typeof Value) ? String (value) : value));
如何序列化某一项值
replacer 作为数组时,结果非常简单,数组的值就代表了将被序列化成 JSON 字符串的属性名
示例代码 1 2 3 4 5 6 7 8 const jsonObj = { name: "JSON.stringify" , params: "obj,replacer,space" }; JSON .stringify(jsonObj, ["params" ]);
参考 MDN JSON stringify
你不知道的 JSON.stringify() 的威力
Something wrong with this article?
Click
here
to submit your revision.
Vector Landscape Vectors by Vecteezy