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

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

问题描述

我想在 HTML5 localStorage 中存储一个 JavaScript 对象,但我的对象显然被转换为字符串.

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

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

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

这是我的代码:

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

控制台输出是

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

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

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

我在 Safari、Chrome 和 Firefox 中看到了这种行为,所以我认为这是我对 HTML5 的误解网络存储规范,而不是浏览器特定的错误或限制.

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.

我试图理解http://www.w3.org/TR/html5/infrastructure.html.我不完全明白它在说什么,但也许我的问题与我的对象的属性不可枚举有关 (???)

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?

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

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.

推荐答案

Apple, MozillaMozilla 再次 文档,该功能似乎仅限于处理字符串键/值对.

Looking at the Apple, Mozilla and Mozilla again 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天全站免登陆