1

I have been working on a project that uses a object to maintain a conversation with the user. However if what has been asked by the user is not in this object. The user is prompted for what should the "AI" answer. The problem is that I can't save this object, so when the user closes the tab or reloads it everything that the "AI" has learned disappears. I've tried using localStorage.setItem('dictionary', JSON.stringify(dictionary)) (dictionary is the object) However it gave me "[object Object]". So i was wondering if there was anyway I could use cookies instead?

J.A.I
  • 49
  • 1
  • 6
  • Possible duplicate: http://stackoverflow.com/questions/11344531/pure-javascript-store-object-in-cookie – Fabian Lurz May 03 '15 at 11:08
  • @J.A.I localstorage must do this for you..you must have missed something..Provide a fiddle for better understanding.. – Rayon May 03 '15 at 11:08
  • 1
    Can you give us an example of what "dictionary" look like? Using JSON would work, but you need to structure the data properly beforehand. – Hayley May 03 '15 at 11:08

2 Answers2

0

the localstorage you use has no problem,did you alert the object and you see the '[object Object]'?

zhou
  • 29
  • 6
0

after you store your object you need to use it like this,

 var dictonaryParsed = JSON.parse(localStorage.getItem('dictionary'));

this will fix your problem, this is because localStorage.setItem() will use the toString() method from the prototype chain, and you will get this [object Object].

This is because everything is converted to string before stored to the key,value pair database(localStorage).

With stringify method that you are using the object will be prepared for the JSON.parse() method if not used you will get something like this "{"a":10}", so you need to Parse your object before using it. If you try to parse not stringyfied object you will get error.

Vitaliy Terziev
  • 6,429
  • 3
  • 17
  • 25