Detailed JSON.stringify and JSON.parse

JSON.stringify()

MDN: JSON.stringify () method to a JavaScript value (object or array) is converted to a JSON string, if specified replacer is a function that can be selectively replaced or if the specified replacer is an array, can be optionally an array containing only the specified attributes.

grammar

// []表示可选参数
JSON.stringify(value[, replacer [, space]])
  • value: To a sequence into a JSONstring value
  • replacer: If the parameter is a function, then the serialization process, by each attribute values are serialized after the conversion and processing of the function; if the parameter is an array, this array containing only the attribute name only into the final serialized JSONstring; if the parameter is null or not provided, all the attributes of the object will be serialized
  • space: Specify a blank string indentation, user beautify output pretty-print; if the argument is a number that represents how much space there; upper limit of 10. If this value is less than 1, it means that no spaces; If the parameter is a string, the string will be as space; if this parameter is not provided, there will be no spaces

return value

It represents a given value JSONstring

description

  • Package type Boolean, numbers, strings are automatically converted to the corresponding process in the sequence of the original value
console.log(
  JSON.stringify({
    flag: new Boolean(false),
    num: new Number(0),
    str: new String('false'),
  }),
);
// 输出 {"flag":false,"num":0,"str":"false"}
  • undefined, And arbitrary function symbolvalues, the sequence of the process will be ignored
console.log(JSON.stringify({ und: undefined, obj: Object }));
// 输出 {}

replacer parameters

replacer parameter may be an array or a function. As a function, it has two parameters, key and value, which will be serialized.

const jsonString = JSON.stringify(
  { name: 'jack', age: 21, gender: 'male' },
  function(key,value){
    return value
  },
  2,
);
console.log(jsonString)
// 输出 
//{
//  "name": "jack",
//  "age": 21,
//  "gender": "male"
//}

space parameters

parameters for controlling the space inside the resulting string spacing. If it is a number, a string of at every level of indentation at a level more than that number of spaces; if it is a string, each level will be more than in the previous indent the string level

console.log(JSON.stringify({ name: 'jack' }, null, 2));
// {
//   "name": "jack"
// }

JSON.parse()

MDN: JSON.parse () method is used to resolve the value of the JavaScript or JSON string, configured by a character string described. Optional before returning to reviver function of performing a transform object obtained

grammar

JSON.parse(text[, reviver])
  • text: To be parsed into JavaScripta string value
  • reviver: Converter, if the passed parameter can be used to modify the original value resolution generated, the timing of the call parsebefore the function returns

return value

objectThis type corresponds to the given jsontext object / value

console.log(JSON.parse('{}'));
console.log(JSON.parse('{"name": "jack"}'))
const obj = JSON.parse('{"name": "jack"}')
console.log(obj.name);
// 输出
// {}
// { name: 'jack' }
// jack

reviver function

If you specify revivera function, JavaScript is parsed value will go through before a conversion will eventually be returned

const obj = JSON.parse('{"name": "jack"}',function(key,value){
  if(key === '') {
    return value
  }      
  return 'change-'+value
})
console.log(obj.name); //change-jack
Published 17 original articles · won praise 0 · Views 392

Guess you like

Origin blog.csdn.net/k19970320j/article/details/104602237