Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use localStorage to store JSON data and read JSON data in Html5

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to use localStorage in Html5 to store JSON data and read JSON data", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "how to use localStorage to store JSON data and read JSON data in Html5".

LocalStorage is a method provided by HTML5 to achieve local storage on the client side, but the localStorage method can only store string data, and sometimes we need to store objects locally such as: JSON;, how can localStorage store and read JSON data?

Idea: since localStorage can only store string data, we can first convert JSON objects into strings and then store them with localStorage methods; when we need to use these JSON data, read them out, and then convert them into JSON objects to take advantage of.

The specific code is as follows:

Var jsonData = {'name':' Zhang San', 'age': 29}; / / define a JSON object var str_jsonData = JSON.stringify (jsonData); console.log (typeof (str_jsonData)); / / stringlocalStorage.setItem (' localData', str_jsonData); / / store string data to 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

Extend:

Stringify () is used to parse a string from an object

Parse () is used to parse the json object from a string.

The above is about the content of this article on "how to use localStorage to store JSON data and read JSON data in Html5". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report