Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Understand JSON. Stringify ()
The json.stringify () method converts a JavaScript object or value to a JSON string, optionally replacing the value if a replacer function is specified, or optionally containing only the properties specified by the array if the specified replacer is an array.
usage
JSON.stringify(value[, replacer [, space]])
Copy the code
value
The value to be serialized into a JSON string.
Replacer optional
If the parameter is a function, each attribute of the serialized value is converted and processed by the function during serialization; If the parameter is an array, only the property names contained in the array will be serialized into the final JSON string; If this parameter is null or not provided, all attributes of the object are serialized.
The function should return a value in a JSON string, as follows:
- If a Number is returned, it is converted to the corresponding string and appended to the JSON string as the property value.
- If a String is returned, that String is appended to the JSON String as an attribute value.
- If a Boolean is returned, “true” or “false” is appended to the JSON string as the property value.
- If any other object is returned, it is recursively serialized into a JSON string, and the replacer method is called for each property. Unless the object is a function, this case will not be serialized as a JSON string.
- If undefined is returned, the property value is not printed in the JSON string.
- Note: You cannot use the replacer method to remove values from an array. If you return undefined or a function, it will be replaced by null.
When replacer bit function
function replacer(key, value) {
if (typeof value === "string") {
return undefined;
}
return value;
}
var foo = {foundation: "Mozilla".model: "box".week: 45.transport: "car".month: 7};
var jsonString = JSON.stringify(foo, replacer);
//{"week":45,"month":7}
Copy the code
When replacer is array
// If replacer is an array, the value of the array represents the property name to be serialized as a JSON string.
JSON.stringify(foo, ['week'.'month']);
/ / '{" week ": 45," month ": 7}', only keep" week "and" month "attribute values.
Copy the code
Space optional
Specifies a blank string for indentation, which is used to beautify the output (pretty-print).
- If the argument is a number, it represents how many Spaces there are; The upper limit is 10. If the value is less than 1, there are no Spaces.
- If the parameter is a string (the first 10 characters are taken when the string is longer than 10 characters), the string will be used as a space.
- If this parameter is not provided (or null), there will be no Spaces.
JSON.stringify({ a: 2 }, null."");
// '{\n "a": 2\n}'
Copy the code
Indent with TAB character (\t) :
JSON.stringify({ uno: 1.dos : 2 }, null.'\t')
/ / '{\
// "uno": 1, \
// "dos": 2 \
/ /} '
Copy the code
Focus on
Json.stringify () converts the value to the appropriate JSON format:
- Convert values if there is a toJSON() method, which defines what values will be serialized.
- Properties of non-array objects are not guaranteed to appear in a serialized string in a particular order.
- Booleans, numbers, and string wrapper objects are automatically converted to their original values during serialization.
- Undefined, arbitrary functions, and symbol values are ignored during serialization (when appearing in an attribute value of a non-array object) or converted to NULL (when appearing in an array). Function/undefined returns undefined when converted separately, such as json.stringify (function(){}) or json.stringify (undefined).
JSON.stringify({x: undefined.y: Object.z: Symbol("")});
/ / '{}'
JSON.stringify([undefined.Object.Symbol("")]);
// '[null,null,null]'
Copy the code
- Executing this method on objects that contain circular references (objects that refer to each other in an infinite loop) throws an error.
- All properties with symbol as the property key are completely ignored, even if they are mandatory in the replacer parameter.
- Date the Date is converted to a string by calling toJSON() (same as date.toisostring ()), so it is treated as a string.
- Values and nulls in NaN and Infinity formats are treated as null.
- Other types of objects, including Map/Set/WeakMap/WeakSet, serialize only enumerable properties.
// Non-enumerable attributes are ignored by default:
JSON.stringify(
Object.create(
null,
{
x: { value: 'x'.enumerable: false },
y: { value: 'y'.enumerable: true}}));// "{"y":"y"}"
Copy the code