The sessionStorage saved object becomes "[object Object]"?

var aa = {a:1,b:{c:2}}
sessionStorage.setItem("aa",aa)
sessionStorage.getItem("aa")
"[object Object]"

when you use sessionStorage to save an object, you can"t get a value, but print it out and find it becomes "[object Object]". What"s the reason?

Sep.24,2021

before saving the data, JSON.stringify () turn the string. Just because you save the object directly, the system uses qiang to zhi to transfer the string for you, that is, [object Object] .


var aa = {a:1,b:{c:2}};
sessionStorage.setItem('aa',JSON.stringify(aa));
bbb = JSON.parse(sessionStorage.getItem('aa'));
console.log(bbb);
The

object is converted to an object when it is first converted to json acquisition.


convert to a json string before saving, and then switch back to


localStorage and sessionStorage can only save strings, and will automatically call the object's toString method

.
Menu