JSON. stringify, toJosn, Json. parse, and json. stringify

Source: Internet
Author: User
Tags tojson

JSON. stringify, toJosn, Json. parse, and json. stringify
JSON. stringify function (JavaScript)

 

Syntax: JSON. stringify (value [, replacer] [, space]) converts JavaScript values to JavaScript Object Notation (Json) strings.
Value is required. The JavaScript value to be converted (usually an object or array ). Replacer is optional. The function or Array Used for the conversion result. If replacer is a function, JSON. stringify calls this function and passes in the keys and values of each member. Use the return value instead of the original value. If this function returns undefined, the Member is excluded. The root object key is an empty string :"". If replacer is an array, only members with key values in the array are converted. The conversion sequence of members is the same as that of keys in the array. When the value parameter is an array, the replacer array is ignored. Space is optional. Add indentation, spaces, and line breaks to the returned JSON text to make it easier to read. If space is omitted, the returned text is generated without any extra space. If space is a number, the returned text is indented at each level to specify the 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 returned text is indented to characters in the string at each level. If space is a string of more than 10 characters, the first 10 characters are used.

If the value has the toJSON method, the JSON. stringify function uses the return value of this method. If the return value of the toJSON method is undefined, no member is converted. This allows the object to determine its own JSON representation.

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

Execution sequence

During the serialization process, if the value parameter pair should have the toJSON method, JSON. stringify will first call the toJSON method. If this method does not exist, the original value is used. Next, if the replacer parameter is provided, the value (original value or toJSON return value) will be replaced with the return value of the replacer parameter. Finally, add spaces to the value based on the optional space parameter to generate the final JSON text.

In this example, the contact object is converted to JSON text using JSON. stringify. Define the memberfilter array to convert only surname and phone members. The firstname member is omitted.
var contact = new Object();contact.firstname = "Jesper";contact.surname = "Aaberg";contact.phone = ["555-0100", "555-0120"];var memberfilter = new Array();memberfilter[0] = "surname";memberfilter[1] = "phone";var jsonText = JSON.stringify(contact, memberfilter, "\t");document.write(jsonText);// Output:// { "surname": "Aaberg", "phone": [ "555-0100", "555-0120" ] }
ToJSON method (Date) (JavaScript) Syntax: objectname. toJSON ()

Objectname

Required. The object to be serialized in JSON format.

The toJSON method is a built-in Member of the Date JavaScript Object. It returns the date string in ISO format (expressed by the suffix Z) in UTC time zone ).

The following example uses the toJSON method to serialize uppercase string Member values. The toJSON method is called when JSON. stringify is called.
JavaScriptvar contact = new Object();contact.firstname = "Jesper";contact.surname = "Aaberg";contact.phone = ["555-0100", "555-0120"];contact.toJSON = 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 = JSON.stringify(contact);/* The value of jsonText is:'{"firstname":"JESPER","surname":"AABERG","phone":["555-0100","555-0120"]}'*/
JSON. parse function (JavaScript)

Converts a JavaScript Object Notation (JSON) string to an object.

Syntax: JSON. parse (text [, reviver])
Text is required. A valid JSON string. Reviver is optional. A function of the conversion 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 situation occurs: If reviver returns a valid value, the member value is 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 usesJSON. parseConverts a JSON string to an object.

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

The following example shows how to useJSON. stringifyConvert the array to a JSON string and then useJSON. parseReturns the string to an array.

var arr = ["a", "b", "c"];var str = JSON.stringify(arr);document.write(str);document.write ("<br/>");var newArr = JSON.parse(str);while (newArr.length > 0) {    document.write(newArr.pop() + "<br/>");}// Output:// ["a","b","c"]// c// b// a

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.