The json.parse () method is used to parse a JSON string, constructing a JavaScript value or object that is described by a string. An optional Reviver function is provided to perform transformations (operations) on the resulting object before returning. Syntax Json.parse (text[, reviver]) parameter text the string to be parsed into JavaScript values to see a description of the JSON syntax for JSON object learning. Reviver Optional If it is a function, it specifies how the original value is parsed and transformed before it is returned. The return value object corresponds to the given JSON text. Exception if the parsed JSON string is illegal, a syntax error exception is thrown. The example uses Json.parse () Json.parse ( ' {} '); // {} json.parse (' true '); // true Json.parse (' "foo"); // "foo" Json.parse (' [1, 5, ' false '] '); // [1, 5, "false"] Json.parse (' null '); // null
The Json.stringify () method converts a JavaScript value (object or array) to a JSON string, and if Replacer is specified as a function, the value can be substituted, or if replacer is specified as an array, Optional includes only the specified properties. The syntax json.stringify (value[, Replacer [, Space]]) parameter value will be serialized into a JSON string. Replacer Optional If the parameter is a function, each property of the serialized value is transformed and processed by the function during serialization, and if the parameter is an array, only the name of the property contained in the array is serialized into the final JSON string;
If the parameter is null or not provided, all properties of the object are serialized, and for more detailed explanations and examples of this parameter, refer to the article using native JSON objects. Space optionally specifies a blank string for indentation, which is used to beautify the output (pretty-print), and if the argument is a number, it represents how many spaces it has; the upper limit is 10. A value less than 1 means there is no space, and if the argument is a string (the first 10 letters of the string), the string is used as a space, and if the argument is not provided (or null), there will be no spaces. The return value is a JSON string that represents the given value.
Json.parse () and Json.stringify ()