JSON and localstorage Love and hate

Source: Internet
Author: User
Tags object object

When using Localstorage, we give a key a value that can be a normal string or an object, and if it is a string, we need to convert it to a JSON string by json.stringify.

But when we modify this JSON string, it is another string into the object, so if we want to get when we do not need to use json.parse, if it is a string, we need to use json.parse, is actually a string, but some need to parse, and some do not need to parse.

var json={"name": ' 1 ', Age: ' 1 '};localstorage.setitem (' 1 '//[Object Object]  Localstorage.setitem (' 2 ', Json.stringify (JSON)); // {"Name": ' 1 ', Age: ' 1 '}


Localstorage[1].name= ' 111 ' can be used as object when modified in 1;
2 values need to be converted to JSON object Json.parse (Localstorage[2]). Name

JSON and localstorage Love and hate

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.