在HTML5 localStorage中存储对象 [英] Storing Objects in HTML5 localStorage

查看:147
本文介绍了在HTML5 localStorage中存储对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在HTML5 localStorage 中存储JavaScript对象,但我的对象显然正在转换为字符串。



我可以使用 localStorage 存储和检索原始JavaScript类型和数组,但对象似乎不起作用。他们应该吗?



以下是我的代码:

  var testObject = {'one' :1,'two':2,'three':3}; 
console.log('typeof testObject:'+ typeof testObject);
console.log('testObject properties:');
for(var test in testObject){
console.log(''+ prop +':'+ testObject [prop]);
}

//将对象放入存储区
localStorage.setItem('testObject',testObject);

//从存储中检索对象
var retrievedObject = localStorage.getItem('testObject');

console.log('typeof retrieveObject:'+ typeof retrievedObject);
console.log('retrieveObject的值:'+ retrieveObject);

控制台输出是

  typeof testObject:object 
testObject属性:
one:1
two:2
three:3
typeof retrievedObject:string
retrieveObject的值:[object Object]

它看起来像我在存储它之前, setItem 方法将输入转换为字符串。



我在Safari,Chrome和Firefox,所以我认为这是我对 HTML5 Web Storage 规范的误解,而不是浏览器 - 特定的错误或限制。



我尝试了解 http://www.w3.org/TR/html5/infrastructure.html 。我不完全明白它的意思,但也许我的问题与我的对象的属性不能枚举(???)

是否有一个简单的解决方法?




更新:W3C最终改变了他们对结构化克隆规范的看法,并决定改变规范以匹配实现。请参阅 https://www.w3.org/Bugs/Public/show_bug。 CGI?ID = 12111 。所以这个问题不再是100%有效的,但答案仍然可能是有趣的。

=http://developer.apple.com/safari/library/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/Name-ValueStorage/Name-ValueStorage.html#//apple_ref/doc/uid/TP40007256-CH6-SW1 = noreferrer> Apple , Mozilla Microsoft 文档,该功能似乎仅限于处理字符串键/值对。



解决方法可以是 stringify

在存储它之前,先对对象进行分析,然后在检索它时解析它:

  var testObject = {'one':1,'two':2,'three':3}; 

//将对象放入存储区
localStorage.setItem('testObject',JSON.stringify(testObject));

//从存储中检索对象
var retrievedObject = localStorage.getItem('testObject');

console.log('retrieveObject:',JSON.parse(retrieveObject));


I'd like to store a JavaScript object in HTML5 localStorage, but my object is apparently being converted to a string.

I can store and retrieve primitive JavaScript types and arrays using localStorage, but objects don't seem to work. Should they?

Here's my code:

var testObject = { 'one': 1, 'two': 2, 'three': 3 };
console.log('typeof testObject: ' + typeof testObject);
console.log('testObject properties:');
for (var prop in testObject) {
    console.log('  ' + prop + ': ' + testObject[prop]);
}

// Put the object into storage
localStorage.setItem('testObject', testObject);

// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');

console.log('typeof retrievedObject: ' + typeof retrievedObject);
console.log('Value of retrievedObject: ' + retrievedObject);

The console output is

typeof testObject: object
testObject properties:
  one: 1
  two: 2
  three: 3
typeof retrievedObject: string
Value of retrievedObject: [object Object]

It looks to me like the setItem method is converting the input to a string before storing it.

I see this behavior in Safari, Chrome, and Firefox, so I assume it's my misunderstanding of the HTML5 Web Storage spec, not a browser-specific bug or limitation.

I've tried to make sense of the structured clone algorithm described in http://www.w3.org/TR/html5/infrastructure.html. I don't fully understand what it's saying, but maybe my problem has to do with my object's properties not being enumerable (???)

Is there an easy workaround?


Update: The W3C eventually changed their minds about the structured-clone specification, and decided to change the spec to match the implementations. See https://www.w3.org/Bugs/Public/show_bug.cgi?id=12111. So this question is no longer 100% valid, but the answers still may be of interest.

解决方案

Looking at the Apple, Mozilla and Microsoft documentation, the functionality seems to be limited to handle only string key/value pairs.

A workaround can be to stringify your object before storing it, and later parse it when you retrieve it:

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

这篇关于在HTML5 localStorage中存储对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆