20

Array of JSON objects are stored in HTML5 localStorage.
For now delimiter is ;
For accessing and modifying array of objects from localStorage, split(';') and join(';') operations used.

However ,delimiter approach looks unstable.
For instance ; could be met inside objects attribute and split(';') operation will be uncorrect.

It could be used ;; for delimiter,but i'm not certain it will be stable also.

Is there any robust way to handle localStorage presented as array of objects,as far localStorage saved as String?

EDIT

one of stoppers is that array of object couldn't be saved to localStorage as classical: "[{},{}]"
localStorage converts it automatially to String like "{},{}"

my current data within localStorage:

"{"name":"volvo","id":"033"};{"name":"saab","id":"034"}"

assumption
perhaps,i can add [ at the start and ] at the end,but it looks not gracefull

sergionni
  • 13,290
  • 42
  • 132
  • 189

2 Answers2

44

Just convert the objects to JSON strings:

localStorage.setItem("savedData", JSON.stringify(objects));

And vice versa:

objects = JSON.parse(localStorage.getItem("savedData")));

Or you can add multiple objects in the same localStorage value:

localStorage.setItem("savedData", JSON.stringify([object1, object2 /*, etc*/]));
object1 = JSON.parse(localStorage.getItem("savedData"))[0];
object2 = JSON.parse(localStorage.getItem("savedData"))[1];

Here's the DOM storage specification.

You can also access savedData like this:

localStorage.savedData = "Hello world"
var foo = localStorage.savedData;

This can be used for both getting and setting the data, but it is considered less "safe" than getItem('name'); and setItem('name', 'value');

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • hi, i've tested with just String in vacuum,it works OK,however problem is that array of JSON objects saved to `localStorage` as delimited by `,` curly brackets,e.g. `"{obj1},{obj2}"` and i expect `"[{obj1},{obj2}]"` – sergionni Dec 04 '12 at 12:04
  • Can you edit the actual object / data into your question? I'm not quite sure how the data you're trying to save looks, now. I'm confused because: `JSON.stringify([{a:1}, {b:2}]);` returns: `"[{"a":1},{"b":2}]"`, like expected. – Cerbrus Dec 04 '12 at 12:07
  • I see. That's not a valid JSON string. Where are you getting this data from? – Cerbrus Dec 04 '12 at 12:12
  • i get it from backend using `TidyJSONWriter`,com.day.cq.commons.TidyJSONWriter – sergionni Dec 04 '12 at 12:13
  • 1
    Hm, I see. The easiest thing to do would be to make sure `TidyJSONWriter` uses `,` as delimiter instead of `;` (If possible), then save that to your `localStorage` with added `[]`: `localStorage.savedData = '['+data+']'` It's kinda ugly but at least it can be properly parsed, then. – Cerbrus Dec 04 '12 at 12:17
0

Read variables:

var xyz = JSON.parse( localStorage.getItem( 'element' ) );

Store variables:

localStorage.setItem( 'element' , JSON.stringify(xyz));

where element is the name of the local storage variable and xyz the name of the js variable.

RRikesh
  • 14,112
  • 5
  • 49
  • 70