I have a very simple dictionary .
var meta = {'foo':'1','moo':'2'}
I want to store this in the local storage and retrieve it.
window.localStorage.setItem("meta", meta);
var meta1 = window.localStorage.getItem("meta");
alert(meta1['foo']);
The above does not work. How can I do it?
localStorageconverts it’s input to strings, so you will have to convert your objects to JSON strings, and back:The reason your code didn’t work is because setting a object in
localStoragesets it’s value to"[object Object]"(object.toString()returns"[object Object]"):