SessionStorage and json objects

phani picture phani · Jan 15, 2016 · Viewed 15.4k times · Source
user: {firstName: "loki", lastName: "ch"}

I am storing this user in session storage.

$window.sessionStorage.user = user;

when i retrieved it back using $window.sessionStorage.user, i got:

[object Object] 

I want in JSON. Any suggestions?

Answer

Mike Cluck picture Mike Cluck · Jan 15, 2016

Convert it to JSON before saving it.

$window.sessionStorage.user = JSON.stringify(user);

Then when you're loading it, parse the JSON.

var user = JSON.parse($window.sessionStorage.user);