JS Web Storage mode

Source: Internet
Author: User
Tags sessionstorage

JSON is one of the most common data formats used in data interactions.

Because the syntax is different for various languages, you can convert arrays, objects, and so on in your own language to JSON strings when passing data
After passing, you can speak a JSON string, parsing it into a JSON object.

The use of JSON objects is basically the same as the objects in JS, the only difference is that the keys in the JSON must be strings.

JSON Name/value pairs

The writing format for JSON data is: name/value pairs.

Eg: "name": "1111"

JSON value

The JSON value can be:

Number (integer or floating point)

String (in double quotes)

Logical value (TRUE or FALSE)

Array (in brackets)

Object (in curly braces)

Null

JSON Numbers

The JSON numbers can be integer or floating-point types:

{"Age": 30}

JSON Object

The JSON object is written in curly braces ({}):

An object can contain multiple Key/value (key/value) pairs.

Key must be a string, and value can be a valid JSON data type (string, number, object, array, Boolean, or null).

A colon (:) split is used in key and value.

Each key/value pair uses commas (,) to split.

{"Name": "1111", "url": "Www.runoob.com"}

Accessing object values

You can use the dot number. To access the value of the object:

Instance

var myObj, X;

MYOBJ = {"Name": "Runoob", "Alexa": 10000, "site": null};

x = Myobj.name;

You can also use brackets [] to access the value of an object:

Instance

var myObj, X;

MYOBJ = {"Name": "Runoob", "Alexa": 10000, "site": null};

x = myobj["name"];

JSON Array

The JSON array is written in brackets:

An array can contain multiple objects:

{

"Sites": [

{"Name": "1111", "url": "Www.runoob.com"},

{"name": "Google", "url": "Www.google.com"},

{"Name": "Weibo", "url": "Www.weibo.com"}

]

}

JSON Boolean value

The JSON Boolean value can be true or false:

{"Flag": true}

JSON NULL

JSON can set a null value:

{"Runoob": null}

Json.parse ()

JSON is typically used to exchange data with the server.

is typically a string when receiving server data.

You can use the Json.parse () method to convert data to JavaScript objects.

Grammar

Json.parse (text[, Reviver])

Parameter description:

Text: Required, a valid JSON string.

Reviver: Optional, a function that transforms the result, which will call this function for each member of the object.

For example, the following data was received from the server:

{"Name": "Runoob", "Alexa": 10000, "site": "Www.runoob.com"}

We use the Json.parse () method to process the above data and convert it to a JavaScript object:

var obj = json.parse (' {' name ': ' Runoob ', ' Alexa ': 10000, ' site ': ' www.runoob.com '} ');

Json.stringify ()

JSON is typically used to exchange data with the server.

is typically a string when sending data to the server.

You can use the Json.stringify () method to convert a JavaScript object to a string.

Grammar

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

For example, we send the following data to the server:

var obj = {"Name": "Runoob", "Alexa": 10000, "site": "Www.runoob.com"};

Use the Json.stringify () method to process the above data and convert it to a string:

var Myjson = json.stringify (obj);

Myjson is a string.

Myjson can be sent to the server:

There are two main ways to add Web Storage to HTML5 :

localstorage and sessionstorage, two objects have no difference in how they are used, the only difference is the effective time to store the data

①localstorage: Unless manually deleted, the data will remain in the local file.

②sessionstorage: Sessinstorage is emptied when the browser is closed.

[Storage data storage]

1, storage can be like ordinary objects, use. Append or read the latest data.

Eg:localstorage = "Zhang San";

2. Commonly used functions

Save data: Localstorage.setitem (Key,value);

Read data: Localstorage.getitem (key);

Delete individual data: Localstorage.removeitem (key);

Delete all data: Localstorage.clear ();

Get the Key:localStorage.key (index) of an index;

Detecting whether the browser supports

if (typeof (Storage)!== "undefined") {

Alert ("Support");

}else{

Alert ("Browser does not support HTML5 storage");

}

JS Web Storage mode

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.