0

I am using localStorage to store some json arrays (no more than 25), and when the user logs out, I need to save the information stored in a MySQL database. Therefore, I am sending the data to a PHP script that is in charge of communicating and dealing with all the database stuff.

Anyway, I have been searching on the web, and I found here - Merge two json/javascript arrays in to one array - that I could just use concat.

I basically use this function:

function saveEverything() {

  var localStorageData = "";

  for (var i=0; i < localStorage.length; i++) {   
    localStorageData = localStorageData.concat(localStorage.getItem(localStorage.key(i)));


  }  

 ... 
}

The ... represents the ajax bit that sends the localStorageData to a PHP script.

As you should know, localStorage doesn't store anything but strings, so I have to do JSON.stringify when I am setting the items. You might have noticed that I didn't do JSON.parse when concatenating the localStorage items into the localStorageData variable. I tried that before, and when I did alert(localStorageData) I only got [Object][object] ... (or something like that).

Anyway, with this kind of approach I am sending strings to php, and each json array is separated by line break. Is this the best/correct thing to do or should I have sticked to the JSON.parse way?

Community
  • 1
  • 1
Larissa Leite
  • 1,358
  • 3
  • 21
  • 36

2 Answers2

1

What does your JSON look like? concat is a method of Array instances, so you can only do this when you're working with an Array. Furthermore, JSON is a notation, to use it like this you would have to parse it back into JavaScript and then out again.

For example,

var json1 = '[{"foo":"bar"}]',
    json2 = '[{"fizz":"buzz"}]';
var mergedJS = JSON.parse(json1).concat(JSON.parse(json2)),
    mergedJSON = JSON.stringify(merged);
mergedJSON; // '[{"foo":"bar"},{"fizz":"buzz"}]'

If they're not Arrays, you might be able to get away with just wrapping them (depending on how you want the result), i.e.

var json1 = '{"foo":"bar"}',
    json2 = '{"fizz":"buzz"}';
var mergedJSON = '[' + json1 + ',' + json2 + ']';
mergedJSON; // '[{"foo":"bar"},{"fizz":"buzz"}]'

Finally,

You might have noticed that I didn't do JSON.parse when concatenating the localStorage items into the localStorageData variable. I tried that before, and when I did alert(localStorageData) I only got [object Object]

[object Object] is the result of calling toString on almost any Object.

({}).toString(); // "[object Object]"

If you want to see something useful, use console.log and view the Console, or convert back to String with JSON.stringify.

Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • for each localStorage item I have something like this {"test":"testing","test2":"testing2","test3":"testing3"} - concat is working fine even without using JSON.parse – Larissa Leite Jan 01 '14 at 18:47
  • @LarissaLeite using `concat` with _String_ is a different method; it is the same as using the `+` operator on a _String_, i.e. `'foo'.concat('bar') === 'foo' + 'bar'; // === "foobar"` – Paul S. Jan 01 '14 at 18:51
  • Yeah, I ended up noticing that, but thanks for the heads up! Good explanation – Larissa Leite Jan 01 '14 at 18:51
1

alert(localStorageData) I only got [Object][object]

That's normal, you should stick with this previous version, build an object of objects and in the end use

JSON.stringify(localStorageData)

to send it as string.

function saveEverything(){ 
    var localStorageData = {}, 
        l = localStorage.length,
        k;
    for (var i=0; i < l; i++) {
        k = localStorage.key(i);
        localStorageData[k] = JSON.parse(localStorage.getItem(k));
    }
    return localStorageData;
}

Using object also makes it easier to distinguish these arrays, since you can also save keys under which they were saved.

dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
  • And then on my PHP script I would do json_encode()? I need to get each one of the json arrays and store them on my database – Larissa Leite Jan 01 '14 at 18:49
  • It depends on how are you soring them, if in some separate fields/tables, then you should `json_decode` it, but if you are storing is as json string then it is already a string. – dev-null-dweller Jan 01 '14 at 18:51