json.stringify function

Source: Internet
Author: User

Excerpt, cool json.stringifyJSON.stringify function (JavaScript)

Converts the JavaScript value to a JavaScript Object notation (Json) string.

Grammar
Json.stringify (value [, Replacer] [, space])
Parameters
Value

Necessary.  the JavaScript value to convert (typically an object or an array).

Replacer

Optional.  the function or array used to convert the result.

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.   

If replacer is an array, only the members with key values in the array are converted.  the members are converted in the same order as the keys in the array.  when the value parameter is also an array, replacer arrays are ignored.

Space

Optional.  adds indents, spaces, and line breaks to the return value JSON text to make it easier to read.

If spaceis omitted, the return value text is generated, without any space cells.

If space is a number, the return value text indents the specified number of spaces at each level.  If space is greater than 10, the text indents 10 spaces.

If space is a non-empty string (for example, "\ T"), the return value text indents the characters in the string at each level.

If space is a string longer than 10 characters, the first 10 characters are used.

Example

This example usesjson.stringifyWillthe contact object is converted to JSON text.  defines an MemberFilter array to convert only surname and phone members.  omit the FirstName member.

Javascript
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 (con Tact, MemberFilter, "\ T");d Ocument.write (jsontext);//output://{"surname": "Aaberg", "Phone": ["555-0100", "555-0120" ] }
Example

This example uses json.stringify with an array.  The Replacetoupper function converts each string in an array to uppercase.

Javascript
var continents = new Array (); Continents[0] = "Europe"; continents[1] = "Asia"; continents[2] = "Australia"; continents[3] = " Antarctica "; continents[4] =" North America ", continents[5] =" South America "; continents[6] =" Africa "; var jsontext = JSON. Stringify (continents, replacetoupper); function Replacetoupper (key, value) {    return value.tostring (). toUpperCase ();} output://"Europe,asia,australia,antarctica,north America,south America,africa"
Example

This example uses the ToJSON method to convert a string value to uppercase.

Javascript
var 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)    {        if (typeof (This[val]) = = = ' string ')            replacement[val] = This[val].touppercase ();        else            replacement[val] = This[val]    }    return replacement;}; var jsontext = json.stringify (Contact);d ocument.write (jsontext);//output:{"FirstName": "JESPER", "surname": "Aaberg" , "Phone": ["555-0100", "555-0120"]} ' {"FirstName": "JESPER", "surname": "Aaberg", "Phone": ["555-0100", "555-0120"]} ' * *
 

json.stringify 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.