LocalStorage store reading JSON how to implement

Xiaobian to share the LocalStorage storage read JSON how to implement, I believe that most people don’t know much, so share this article to everyone, I hope everyone is reading this article, there is a big harvest, below Let’s go find it!

LocalStorage is a method of re-client to implement local storage, but the localStorage method can only store string data, sometimes we need to store objects to local, for example,: json; then, how to implement how LocalStorage can be implemented What is the storage and reading of JSON data?

Idea: Since LocalStorage can only store string data, then we can first convert JSON objects into strings, then store them with localStorage methods; wait until these JSON data is required, first read them first Take it out, then convert it to JSON objects.

The specific code is as follows:

VAR JSONDATA = {‘Name’: ‘Zhang 3’, ‘Age’: 29}; // Defines a JSON Object VAR STR_JSONDATA = JSON.stringify (jsonData); console.log (typeof (str_jsonData)); // stringlocalStorage.setItem ( ‘localData’, str_jsonData); // string data stored in the local var getLocalData = localStorage.getItem ( ‘localData’) ; // read string data console.log (typeof (getlocaldata)); // stringvar jsonobj = json.parse (getLocaldata); console.log (TypeOf (jsonobj)); // objconsole.log (jSONOBJ.AGE); // 29

 The above is the LocalStorage store reads all the contents of JSON implementation, thank you for reading! I believe everyone has a certain understanding, I hope to share the content of everyone, if you want to learn more knowledge, please pay attention to Tumi Yun Industry News Channel!                      
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment