search

Home  >  Q&A  >  body text

Can sessionStorage only store strings?

sessionStorage.setItem('xxx',true), get the result "true" string
sessionStorage.setItem('xxx',{}), get the result "[object Object]"
How Save other types?

PHP中文网PHP中文网2782 days ago553

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-05-18 10:55:07

    sessionStorage.setItem('xxx',JSON.stringify({}));

    reply
    0
  • 迷茫

    迷茫2017-05-18 10:55:07

    sessionStoragelocalStorage只能存字符串,针对对象需要存储storageIf you want, you can serialize it to achieve the effect:

    var obj = {};
    sessionStorage.setItem('xxx', JSON.stringify(obj)); //序列化
    obj = JSON.parse(sessionStorage.setItem('xxx')); //这时候需要反序列化一下

    Reference:

    • localStorage stores JSON objects

    • JS operation JSON

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-18 10:55:07

    webStorage itself can store non-strings, but no browser has implemented it yet

    reply
    0
  • Cancelreply