user2085143 user2085143 - 1 year ago 50
JSON Question

JSON Parse from LocalStorage error

I am storing an object in localStorage using the following

function onExit(){
localStorage.setItem("my_object","'" + JSON.stringify(object) + "'");

When logging this out of localStorage it looks like this


Now if I call JSON.parse on this directly it works, that is to say


will give me an object. But if I try


I get the 'unexpected character at line 1 of JSON data' error message

Where am I going wrong? Note: I have tried not enclosing the object in single quotes to no effect.

Answer Source

There is no need to wrap JSON.stringify(object) with the extra quotes ("'") as it returns a usable string.


To retrive and decode the JSON object, you need to call getItem

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download