0

I have an associative array which looks something like this (not actual code):

sort_order['14682007'][0] = "1"
sort_order['14682007'][1] = "0"
sort_order['82501220'][0] = "2"
sort_order['82501220'][1] = "1"
sort_order['82501220'][2] = "0"

I'd like to serialize this array this way:

sort_order = JSON.stringify(sort_order);

But it returns only an empty string. Colud some one help me?

ACs
  • 1,325
  • 2
  • 21
  • 39

1 Answers1

3

Make sure you declare your variables properly, an example:

    var sort_order = {};
    sort_order['82501220'] = [];
    sort_order['14682007'] = [];
    sort_order['14682007'][0] = "1";
    sort_order['14682007'][1] = "0";
    sort_order['82501220'][0] = "2";
    sort_order['82501220'][1] = "1";
    sort_order['82501220'][2] = "0";

    console.log(JSON.stringify(sort_order));

Note: If you declare sort_order as an array in this case you will have a very long sparse array I think.

paperstreet7
  • 2,058
  • 1
  • 15
  • 18
  • I totally agree with this answer. I just recreated this example with `sort_order = [];`. At last when I did `JSON.stringify(sort_order);` the browser memory increases and crashes since it is a very huge sparse array. +1 for the brilliant (and very important) input – Ashwin Dec 10 '13 at 07:52