javascript - Comment stocker des objets dans HTML5 localStorage

Mots clés : javascripthtmllocal-storagejavascript

meilleur 5 Réponses javascript - Comment stocker des objets dans HTML5 localStorage

vote vote

99

var testObject = { 'one': 1, 'two': 2, 'three': 3 };  // Put the object into storage localStorage.setItem('testObject', JSON.stringify(testObject));  // Retrieve the object from storage var retrievedObject = localStorage.getItem('testObject');  console.log('retrievedObject: ', JSON.parse(retrievedObject)); 
vote vote

80

Storage.prototype.setObject = function(key, value) {     this.setItem(key, JSON.stringify(value)); }  Storage.prototype.getObject = function(key) {     var value = this.getItem(key);     return value && JSON.parse(value); } 
vote vote

78

Storage.prototype.setObject = function(key, value) {     this.setItem(key, JSON.stringify(value)); }  Storage.prototype.getObject = function(key) {     return JSON.parse(this.getItem(key)); } 
vote vote

68

var data = {   set: function(key, value) {     if (!key || !value) {return;}          if (typeof value === "object") {       value = JSON.stringify(value);     }     localStorage.setItem(key, value);   },   get: function(key) {     var value = localStorage.getItem(key);          if (!value) {return;}      // assume it is an object that has been stringified     if (value[0] === "{") {       value = JSON.parse(value);     }      return value;   } } 
vote vote

50

//Objects and Arrays:     var obj = {key: "value"};     localStorage.object = JSON.stringify(obj);  //Will ignore private members     obj = JSON.parse(localStorage.object); //Boolean:     var bool = false;     localStorage.bool = bool;     bool = (localStorage.bool === "true"); //Numbers:     var num = 42;     localStorage.num = num;     num = +localStorage.num;    //short for "num = parseFloat(localStorage.num);" //Dates:     var date = Date.now();     localStorage.date = date;     date = new Date(parseInt(localStorage.date)); //Regular expressions:     var regex = /^No\.[\d]*$/i;     //usage example: "No.42".match(regex);     localStorage.regex = regex;     var components = localStorage.regex.match("^/(.*)/([a-z]*)$");     regex = new RegExp(components[1], components[2]); //Functions (not recommended):     function func(){}     localStorage.func = func;     eval( localStorage.func );      //recreates the function with the name "func" 
//Object with private and public members:     function MyClass(privateContent, publicContent){         var privateMember = privateContent || "defaultPrivateValue";         this.publicMember = publicContent  || "defaultPublicValue";          this.toString = function(){             return '{"private": "' + privateMember + '", "public": "' + this.publicMember + '"}';         };     }     MyClass.fromString = function(serialisedString){         var properties = JSON.parse(serialisedString || "{}");         return new MyClass( properties.private, properties.public );     }; //Storing:     var obj = new MyClass("invisible", "visible");     localStorage.object = obj; //Loading:     obj = MyClass.fromString(localStorage.object); 
var obj = {}; obj["circular"] = obj; localStorage.object = JSON.stringify(obj);  //Fails 
var obj = {id: 1, sub: {}}; obj.sub["circular"] = obj; localStorage.object = JSON.stringify( obj, function( key, value) {     if( key == 'circular') {         return "$ref"+value.id+"$";     } else {         return value;     } }); 

Questions similaires