SoFunction
Updated on 2025-04-05

A brief discussion on stringify function, toJosn function and parse function in JSON

Functions (JavaScript)

Syntax: (value [, replacer] [, space])

Converts JavaScript values ​​to JavaScript object notation (Json) string.

Copy the codeThe code is as follows:

value
Required. The JavaScript value to convert (usually an object or an array).
replacer
Optional. Function or array used to convert results.
If replacer is a function, the function is called and the key and value of each member are passed in. Use the return value instead of the original value. If this function returns undefined, members are excluded. The key of the root object is an empty string: "".
If replacer is an array, only members with key values ​​in that array are converted. The conversion order of members is the same as the order of keys in the array. When the value parameter is also an array, the replacer array is ignored.
space
Optional. Add indents, spaces, and line breaks to the return value JSON text to make it easier to read.
If space is omitted, the return value text will be generated without any extra spaces.
If space is a number, the return value text is indented at each level by a specified number of spaces. If space is greater than 10, the text is indented by 10 spaces.
If space is a non-empty string (for example, "\t"), the return value text is indented into the characters in the string in each level.
If space is a string with a length greater than 10 characters, the first 10 characters are used.

If value has a toJSON method, the function will use the return value of that method. If the return value of the toJSON method is undefined, no members are converted. This enables the object to determine its own JSON representation.

Values ​​that do not have JSON representations will not be converted, such as undefined. In the object, these values ​​are discarded. In the array, these values ​​are replaced with null.

Execution order

During serialization, if the value parameter corresponds to a toJSON method, the toJSON method will be called first. If the method does not exist, the original value is used. Next, if the replacer parameter is provided, the value (or the original value or toJSON return value) will be replaced with the return value of the replacer parameter. Finally, add spaces to the value according to the optional space parameter to generate the final JSON text.

This example uses Convert a contact object to JSON text. Define the memberfilter array so that only the Surname and phone members are converted. Omit the firstname member.

Copy the codeThe code is as follows:

var contact = new Object();
= "Jesper";
= "Aaberg";
= ["555-0100", "555-0120"];
var memberfilter = new Array();
memberfilter[0] = "surname";
memberfilter[1] = "phone";
var jsonText = (contact, memberfilter, "\t");
(jsonText);
// Output:
// { "surname": "Aaberg", "phone": [ "555-0100", "555-0120" ] }

toJSON method (Date) (JavaScript)

grammar:()

objectname

Required. Objects that require JSON serialization.

The toJSON method is a built-in member of the Date JavaScript object. It returns an ISO format date string (denoted by the suffix Z) of the UTC time zone.

The following example uses the toJSON method to serialize uppercase string member values. Call the toJSON method when called.

Copy the codeThe code is as follows:

JavaScript
var contact = new Object();
= "Jesper";
= "Aaberg";
= ["555-0100", "555-0120"];
= function(key)
 {
    var replacement = new Object();
    for (var val in this)
    {
        if (typeof (this[val]) === 'string')
            replacement[val] = this[val].toUpperCase();
        else
            replacement[val] = this[val]
    }
    return replacement;
};
var jsonText = (contact);
/* The value of jsonText is:
'{"firstname":"JESPER","surname":"AABERG","phone":["555-0100","555-0120"]}'
*/

Functions (JavaScript)

Convert JavaScript Object Notation (JSON) string to object

Syntax: (text [, reviver])

Copy the codeThe code is as follows:

text
Required. A valid JSON string.
reviver
Optional. A function that converts the result. This function will be called for each member of the object. If a member contains a nested object, the nested object is converted before the parent object. For each member, the following happens:
If reviver returns a valid value, the member value will be replaced with the converted value.
If reviver returns the same value it receives, the member value is not modified.
If reviver returns null or undefined, the member is deleted.

The following example uses to convert a JSON string to an object.

Copy the codeThe code is as follows:

var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';
var contact = (jsontext);
( + ", " + );
// Output: Aaberg, Jesper

The following example demonstrates how to convert an array to a JSON string using , and then reconvert the string to an array using .

Copy the codeThe code is as follows:

var arr = ["a", "b", "c"];
var str = (arr);
(str);
("<br/>");
var newArr = (str);
while ( > 0) {
    (() + "<br/>");
}
// Output:
// ["a","b","c"]
// c
// b
// a