Phonegap localStorage返回字符串“[object Object]”而不是对象

时间:2011-04-01 11:08:15

标签: javascript html5 object cordova local-storage

我试图在Phonegap中使用localStorage对象,但getItem只接收字符串"[object Object]"而不是获取对象:

var storage = window.localStorage;
storage.setItem('test',{'name':'mark','greeting':'Hello'});
console.log(storage.getItem('test'));

在Google Chrome控制台中,它说:

[object Object]

“console.log(storage)”的输出如下:

Storage
...
test: "[object Object]"

如果我尝试访问对象的属性,它只是说“未定义”:

storage.getItem('test').name

任何想法如何让这个工作?

1 个答案:

答案 0 :(得分:36)

HTML5 localStorage允许您存储strings only

您需要在存储对象时执行JSON.stringify,并在检索对象时执行JSON.parse