Json. Stringify is often asked during the front-end interview of a large factory, so it is essential to skillfully use and master it.

So, what exactly is the json.stringify method?

Methods Basic introduction

Json. stringify is a method in a JSON object that is often used in everyday development. A JSON object contains two methods:

  1. Parse () for parsing into a JSON object;
  2. Stringify () used to convert an object to a JSON string method.

Let’s look at the basic use of each method.

JSON.parse

The json. parse method parses a JSON string and constructs JavaScript values or objects described by the string. This method takes two arguments: the first argument is the JSON string that needs to be parsed, and the second argument is an optional argument that provides the optional reviver function to transform the resulting object before returning it.

Parse (text[, reviver])

Let’s take a look at this method and the use of the Reviver parameter in some code, as shown below.

const json = '{"result":true, "count":2}';
const obj = JSON.parse(json);
console.log(obj.count);
/ / 2
console.log(obj.result);
// true
/* Case with second argument */
JSON.parse('{"p": 5}'.function (k, v) {
    if(k === ' ') return v;     // If k is not empty,
    return v * 2;              // return 2 times the value of the property
});                            // { p: 10 }
Copy the code

The above code shows that we can convert a string in JSON format into an object return; With the second argument, you can manipulate the string, such as multiplying the value of the property by 2 in the example above.

Let’s look at the basics of json.Stringify.

JSON.stringify

The json. stringify method converts a JavaScript object or value to a JSON string. By default, this method actually takes three arguments: the first argument is mandatory, and the last two are optional but not mandatory. The first argument passed is the object to be converted; The second is a replacer function. If the specified replacer is an array, it optionally handles only properties that contain the array specified. The third argument controls the spacing in the result string, and the last two arguments are used less generally.

The syntax for this method is: json.stringify (value[, replacer [, space]])

Let’s take a look at the next few arguments in some code, as shown below.

JSON.stringify({ x: 1.y: 2 });
// "{"x":1,"y":2}"
JSON.stringify({ x: [10.undefined.function(){}, Symbol(' ')]})// "{"x":[10,null,null,null]}"
/* An example of the second argument */
function replacer(key, value) {
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}
var foo = {foundation: "Mozilla".model: "box".week: 4.transport: "car".month: 7};
var jsonString = JSON.stringify(foo, replacer);
console.log(jsonString);
// "{"week":4,"month":7}"
/* An example of the third argument */
JSON.stringify({ a: 2 }, null."");
/* "{ "a": 2 }"*/
JSON.stringify({ a: 2 }, null."");
// "{"a":2}"
Copy the code

In the above code, you can see what happens when the second argument, replacer, is added: The substitution method filters out the string properties in the object, and the numeric properties returned after stringify become strings. When multiple Spaces are passed in as the third argument, it increases the number of Spaces in the resulting string, which can be seen in the last piece of code.

If this content is helpful to you, help click this link, let xiaobian add a chicken leg yo! github.crmeb.net/u/xingfu