Localstorage data acquisition and processing methods

Source: Internet
Author: User
About strings and objects
1.localStorage can only save strings, if the object directly into the Localstorage, then directly save the string object
2. If you call Localstorage's GetItem ("carts") method, the information obtained is also a string
3.js and String conversions
JS Object----> string: var str=json.stringfy (JS object)
String----->js object: var obj=json.parse (string);

such as strings and objects
Class Person extends object{
String name;
}

Person P=new person (); This is a Java object, converted to a string, using the ToString method


In short, from the local to get the data is a string, first from the string to the JS object (array), the same, the end of the data to be converted into a string and then stored locally, the code is as follows:

var books=localstorage.getitem ("carts");//Get String
var bookarr=json.parse (books);
var s=json.stringify (Bookarr);
Localstorage.setitem ("carts", s);


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.