LocalStorage is a method of re -client provided by HTML5 to achieve local storage, but the LocalStorage method can only store string data. Sometimes we need to store objects to local, such as JSON; then how can localStorage realize the storage and reading of JSON data What about it?
Idea: Since LocalStorage can only store string data, we can first convert the JSON object into string, and then store it with the LocalStorage method; when these JSON data need Use the JSON object to use it.
The specific code is as follows:
var jsondata = {'name': 'Zhang San', 'Age': 29}; // Define a json object var sTR_JSNDATA = json.stringify (jsondata); console.log (Typeof (str_jsondata)); // Str. IngLocalStorage. SetItem ('LocalData', Str_JSONDATA); // Storage string data to the local vargLOCALDATA = LOCALSTORAGE.GetITEM ('LocalData'); ocaldata); // Stringvar JSONOBJ = Json.parse (getlocalData); console.log (typeof (jsonObj)); // objconsole.log (jsonObj.age); // 29Extension:
Stringify () is used to resolve the string from an object;
PARSE () is used to resolve JSON objects from a string.
The above is the LocalStorage storage JSON data in HTML5 introduced by Xiaobian and read the implementation method of JSON data. I hope it will be helpful to everyone. If you have any questions, please leave me a message. Xiaobian will reply to everyone in time. Thank you very much for your support for the VEVB Wulin website!