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

How to store JSON data in sessionstorage

angryTom
angryTomforward
2019-12-05 17:44:192936browse

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);

Get sessionstorage

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

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));

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);

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete