开发者

Phonegap localStorage returns the string "[object Object]" instead of the object

开发者 https://www.devze.com 2023-02-21 01:45 出处:网络
I trief to use the localStorage object in Phonegap, but instead of getting an object, the getItem only recei开发者_C百科ves a string \"[object Object]\":

I trief to use the localStorage object in Phonegap, but instead of getting an object, the getItem only recei开发者_C百科ves a string "[object Object]":

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

In the console of Google chrome it says:

[object Object]

The output of "console.log(storage)" is the following:

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

If I try to access a property of the object it just says "undefined":

storage.getItem('test').name

Any ideas how to get this to work?


HTML5 localStorageallows you to store strings only.

You'll have to perform a JSON.stringify when you store your object, and JSON.parse when you retrieve it.

0

精彩评论

暂无评论...
验证码 换一张
取 消