Home > Web Front-end > JS Tutorial > How to store JSON data in sessionstorage

How to store JSON data in sessionstorage

angryTom
Release: 2019-12-05 17:44:19
forward
3003 people have browsed it

How to store JSON data in sessionstorage

How to store JSON data in sessionstorage

In web development, sessionstorage may often be used to store data and store single string data It is not difficult to use variables

var str = 'This is a string';
sessionstorage.setItem('param',str);
Copy after login

Get sessionstorage

var item = sessionstorage.getItem('param');
console.log(item);
Copy after login

But sessionStorage can only store string type data and cannot directly store array types and JSON objects. What should I do if there is a need? It's actually very simple.

First convert the JSON object into a string through the JSON.stringify() method, and then store it in sessionstorage

var obj = {"name": "Tom","age": 12,"gender": "man"};
sessionstorage.setItem('jsonParams',JSON.stringify(obj));
Copy after login

Then convert the string into a string through the JSON.parse() method JSON format is enough

var data = JSON.parse(sessionstorage.getItem('jsonParams'));
console.log(data);
Copy after login

This article comes from the js tutorial column, welcome to learn!

The above is the detailed content of How to store JSON data in sessionstorage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template