Talking about JSON.parse() and JSON.stringify()

JSON.parse()

  • JSON is usually used to exchange data with the server
  • It is generally a string when receiving server data
  • We can use the JSON.parse() method to convert the data into JavaScript objects

grammar

JSON.parse(text[, reviver])
  • text: required, a valid JSON string
  • reviver: Optional, a function that transforms the result, this function will be called for each member of the object

abnormal

Analytical data

  • JSON cannot store Date objects
  • If you need to store a Date object, you need to convert it to a string and then convert the string to a Date object

It is not recommended to use functions in JSON

JSON.stringify()

  • JSON is usually used to exchange data with the server
  • It is generally a string when sending data to the server
  • We can use the JSON.stringify() method to convert JavaScript objects to strings

grammar

JSON.stringify(value[, replacer[, space]])
  • value: required, the JavaScript value to be converted (usually an object or array).
  • replacer: optional. The function or array used to convert the result.
  • If replacer is a function, JSON.stringify
    will call the function and pass in the key and value of each member. Use the return value instead of the original value. If this function returns
    undefined, the member is excluded. The key of the root object is an empty string: ""
  • If replacer is an array, only members with key values ​​in the array are converted. The conversion order of the members is the same as the order of the keys in the array. When the value parameter is also an array, the replacer array will be ignored.
  • space: Optional. Add indentation, space and line break to the text. If space is a number, the return value text will be indented by a specified number of spaces at each level. If space is greater than 10, the text will be indented by 10 spaces. Space can also use non-digits, such as: \t.

abnormal

Analytical data

  • JSON cannot store Date objects
  • JSON.stringify() will convert all dates to strings

It is not recommended to use functions in JSON

to sum up

JSON.parse(): Convert a string to an object

JSON.stringify(): Convert an object to a string

Guess you like

Origin blog.csdn.net/weixin_43956521/article/details/111557560