Talking about json.stringify function and tojosn function and Json.parse function

Source: Internet
Author: User
Tags tojson

json.stringify function (JavaScript)

Syntax: json.stringify (value [, Replacer] [, space])converts the JavaScript value to a JavaScript Object notation (Json) string.
value is required. The JavaScript value to convert (typically an object or an array). Replacer is optional. The function or array used to convert the result. If Replacer is a function, Json.stringify calls the function and passes in the key and value of each member. Use the return value instead of the original value. If this function returns undefined, the member is excluded. The key of the root object is an empty string:"" ten characters.

Ifvalue has the ToJSON method, the json.stringify function uses the return value of the method. if The return value of the ToJSON method is undefined, the member is not converted. This enables the object to determine its own JSON representation.

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

Execution order

During serialization, if theThe value parameter pair should have the ToJSON method, then json.stringify will call the ToJSON method first. If the method does not exist, the original value is used.Next, if you supply the Replacer parameter, the value (original value or ToJSON return value) is replaced with the return value of the Replacer parameter. Finally, a space is added to the value according to the optional space parameter to produce the final JSON text.

This example usesJson.stringify Converts the contact object to JSON text. defines an MemberFilter array to convert only surname and phone members. omit the FirstName member.
varContact =NewObject (); Contact.firstname="Jesper"; Contact.surname="Aaberg"; Contact.phone= ["555-0100","555-0120"];varMemberFilter =NewArray (); memberfilter[0] ="Surname"; memberfilter[1] ="Phone";varJsontext = json.stringify (Contact, MemberFilter,"\ t");d ocument.write (jsontext);//Output://{"Surname": "Aaberg", "Phone": ["555-0100", "555-0120"]}
ToJSON Method (Date) (JavaScript) Syntax: Objectname.tojson ()

ObjectName

Necessary. the object that needs to be JSON serialized.

tojson method is a built-in member of the date JavaScript object." The tojson  method is a built-in member of the  date javascript object.  

The following example uses the ToJSON method to serialize uppercase string member values. The ToJSON method is called when Json.stringify is called .
JavaScriptvarContact =NewObject (); Contact.firstname="Jesper"; Contact.surname="Aaberg"; Contact.phone= ["555-0100","555-0120"];contact.tojson=function (key) {varReplacement =NewObject ();  for(varValinch  This)    {        if(typeof( This[Val]) ==='string') Replacement[val]= This[Val].touppercase (); ElseReplacement[val]= This[Val]}returnreplacement;};varJsontext =json.stringify (contact);/*The value of Jsontext is: ' {"FirstName": "JESPER", "surname": "Aaberg", "Phone": ["555-0100", "555-0120"]} '*/
Json.parse function (JavaScript)

To convert a JavaScript object Notation (JSON) string to an object

Syntax:Json.parse (text [, Reviver])
null or undefined, the member is deleted.

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

var ' {"FirstName": "Jesper", "surname": "Aaberg", "Phone": ["555-0100", "555-0120"]} ' ; var contact ="" + contact.firstname); // Output:aaberg, Jesper

The following example demonstrates how to use json.stringify to convert an array to a JSON string and then use json.parse to convert the string to a group.

vararr = ["a","b","C"];varstr =json.stringify (arr);d ocument.write (str);d Ocument.write ("<br/>");varNEWARR =json.parse (str); while(Newarr.length >0) {document.write (Newarr.pop ()+"<br/>");}//Output://["A", "B", "C"]//C//b//a

Talking about json.stringify function and tojosn function and Json.parse function

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.