Json.stringify ()

Source: Internet
Author: User

Json.stringify ()

In this article

Overview

The json.stringify () method serializes any JavaScript value to a JSON string.

Grammar

Json.stringify (value[, replacer [, Space]])

Parameters

Value

The value that will be serialized into a JSON string.

Replacer options available

If the parameter is a function, each property of the serialized value is transformed and processed by the function during serialization, and if the parameter is an array, only the name of the property contained in the array is serialized into the final JSON string. For more detailed explanations and examples of this parameter, refer to the article using native JSON objects.

Space options available

Specifies a blank string for indentation, which is used to beautify the output (pretty-print).

Describe

There are five things to note about serialization:

    • Properties of non-array objects are not guaranteed to appear in a specific order in the serialized string.
    • The wrapped object of a Boolean, number, or string is automatically converted to the corresponding original value during serialization.
    • Undefined, arbitrary functions, and symbol values are ignored during serialization (when they occur in property values of non-array objects) or converted to null (when appearing in an array).
    • All attributes with the Symbol property key are completely ignored, even if the Replacer parameter is mandatory to include them.
    • Non-enumerable properties are ignored

Json.stringify ({}); ‘{}‘

Json.stringify (TRUE); ' True '

Json.stringify ("foo"); ' "foo" '

Json.stringify ([1, "false", false]); ' [1, ' false ', false] '

Json.stringify ({x:5}); ' {' X ': 5} '

Json.stringify ({x:5, y:6});

' {' X ': 5, ' Y ': 6} ' or ' {' Y ': 6, ' X ': 5} ' May

Json.stringify ([New Number (1), New String ("false"), new Boolean (false)]);

' [1, ' false ', false] '

Json.stringify ({x:undefined, y:object, Z:symbol ("")});

‘{}‘

Json.stringify ([Undefined, Object, Symbol ("")]);

' [Null,null,null] '

Json.stringify ({[Symbol ("foo")]: "foo"});

‘{}‘

Json.stringify ({[Symbol.for ("foo")]: "foo"}, [Symbol.for ("foo")]);

‘{}‘

Json.stringify ({[Symbol.for ("foo")]: "foo"}, function (k, v) {

if (typeof k = = = "Symbol") {

Return "a symbol";

}

});

‘{}‘

Non-enumerable properties are ignored by default:

Json.stringify (Object.create (null, {x: {value: ' X ', enumerable:false}, Y: {value: ' Y ', Enumerable:true}}));

' {' Y ': ' Y '} '

Space parameter

The space parameter is used to control the spacing inside the resulting string. If it is a number, at the time of the string, each level will be indented more than the previous level (up to 10 spaces), and if it is a string, each level will be indented with the string (or the first 10 characters of the string) than the previous level.

Json.stringify ({a:2}, NULL, ""); ' {\ n ' a ': 2\n} '

Use the tab (\ T) to indent:

Json.stringify ({uno:1, dos:2}, NULL, ' \ t ')

‘{ \

"Uno": 1, \

"DOS": 2 \

}‘

ToJSON method

If a serialized object has the ToJSON method, the ToJSON method overrides the default serialization behavior of the object: The return value after the call to the ToJSON method is serialized, not the object being serialized, for example:

var obj = {

Foo: ' foo ',

Tojson:function () {

return ' bar ';

}

};

Json.stringify (obj); ' Bar '

Json.stringify ({x:obj}); ' {' x ': ' Bar '} '

Examples of using json.stringify combined with Localstorage

At some point, you want to store an object created by the user, and you can recover the object even after the browser is closed. The following example is a template that json.stringify applies to this scenario:

Create a sample data

var session = {

' Screens ': [],

' State ': true

};

Session.screens.push ({"Name": "Screena", "width": (), "height": 250});

Session.screens.push ({"Name": "Screenb", "width": 650, "height": 350});

Session.screens.push ({"Name": "Screenc", "width": +, "height": 120});

Session.screens.push ({"Name": "Screend", "width": +, "height": 60});

Session.screens.push ({"Name": "Screene", "width": 390, "height": 120});

Session.screens.push ({"Name": "Screenf", "width": 1240, "height": 650});

Convert to JSON string using json.stringify

Then use Localstorage to save the session name.

Localstorage.setitem (' Session ', json.stringify (session));

Then how to convert the string generated by json.stringify, which is stored in JSON format in Localstorage

var restoredsession = json.parse (Localstorage.getitem (' Session '));

Now restoredsession contains the objects stored in the localstorage.

Console.log (restoredsession);

Specification

Canonical name and link

Canonical state

ECMAScript 5.1 (ECMA-262)
Json.stringify

Standard

ECMAScript (6th Edition, ECMA-262)
Json.stringify

Standard

Browser compatibility

    • Desktop

    • Mobile

Feature

Chrome

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic Support

(Yes)

3.5 (1.9.1)

8.0

10.5

4.0

RELATED LINKS

    • Using native JSON objects

Json.stringify ()

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.