跳到主要内容

JSON

JSON.parse

JSON.parse(text[, reviver])

text: JSON 字符串

reviver:转换函数

If a function, this prescribes how the value originally produced by parsing is transformed, before being returned.

JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', (key, value) => {
console.log(key, JSON.stringify(value));
return typeof value === 'number' ? 2 * value : value;
});
// 1 1
// 2 2
// 4 4
// 6 6
// 5 {"6":12}
// 3 {"4":8,"5":{"6":12}}
// "" {"1":2,"2":4,"3":{"4":8,"5":{"6":12}}}
{
"1": 2,
"2": 4,
"3": {
"4": 8,
"5": {
"6": 12
}
}
}

JSON.stringify

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

value:需要转换的值

replacer:可以是数组或者是函数,为数组时将会只保存数组对应的 key,为函数时接受两个参数,key 和 value,根据返回值决定结果

A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string.

  • If you return a Number, the string corresponding to that number is used as the value for the property when added to the JSON string.
  • If you return a String, that string is used as the property's value when adding it to the JSON string.
  • If you return a Boolean, "true" or "false" is used as the property's value, as appropriate, when adding it to the JSON string.
  • If you return null, null will be added to the JSON string.
  • If you return any other object, the object is recursively stringified into the JSON string, calling the replacer function on each property, unless the object is a function, in which case nothing is added to the JSON string.
  • If you return undefined, the property is not included (i.e., filtered out) in the output JSON string.

space:为数字时,设置占位空格的长度,最大值为 10,小于 1 忽略。为字符串时,设定字符串为占位符,字符串最大长度为 10。

A String or Number object that's used to insert white space into the output JSON string for readability purposes.

If this is a Number, it indicates the number of space characters to use as white space; this number is capped at 10 (if it is greater, the value is just 10). Values less than 1 indicate that no space should be used.

If this is a String, the string (or the first 10 characters of the string, if it's longer than that) is used as white space. If this parameter is not provided (or is null), no white space is used.

转换注意点:

  • 如果转换值存在 toJSON 方法,则先调用。

    If the value has a toJSON() method, it's responsible to define what data will be serialized.

  • Boolean, Number, String 对象转换为对应的原始值。

    Boolean, Number, and String objects are converted to the corresponding primitive values during stringification, in accord with the traditional conversion semantics.

  • 如果遇到 undefined,Function,Symbol,如果是对象的属性则直接被忽略(删除),如果是数组元素则返回 null,如果是转换值本身那会被转换成 undefined 字符 串

    If undefined, a Function, or a Symbol is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array). JSON.stringify() can also just return undefined when passing in "pure" values like JSON.stringify(function(){}) or JSON.stringify(undefined).

  • 所有的 Symbol 为 key 的属性将会为忽略(删除),哪怕是使用了 replacer 函数。

    All Symbol-keyed properties will be completely ignored, even when using the replacer function.

  • Date 对象存在 toJSON 方法,将会被转换成字符串

    The instances of Date implement the toJSON() function by returning a string (the same as date.toISOString()). Thus, they are treated as strings.

  • Infinity 和 NaN 都会被视为 null

    The numbers Infinity and NaN, as well as the value null, are all considered null.

  • 所有的其它 Object(Map,Set,WeakMap,WeakSet)将只留下可枚举属性。

    All the other Object instances (including Map, Set, WeakMap, and WeakSet) will have only their enumerable properties serialized.