2

I have a javascript object that contains a few objects that contain associative arrays. I've been trying to use the json2.js library's stringify function but the output doesn't contain the arrays held in the contained object members. In my code I start with something like this

obj = {"arr1" : [], "arr2" : [], "arr3" : []};

then I add to it with loops that fill each of the contained arrays

obj[arr*].push[arritem*];
obj[arr*][arritem*] = something;

The arr* and arritem* I put in just to represent the variable I am putting in for the loops. I try Json.stringify(obj) but the string I get back is

'{"arr1" : [0], "arr2" : [0], "arr3" : [0]}'

I would like to see the ouput as

'{"arr1" : [ "arritem1" : something, "arritem2" : something2], "arr2" : [ "arritem1" : something, "arritem2" : something2], "arr3" : [ "arritem1" : something, "arritem2" : something2]}'

is there a better library for this or is there something I have to do before strinfying?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
gman060692
  • 23
  • 1
  • 4

2 Answers2

4
var obj = {"arr1" : [], "arr2" : [], "arr3" : []};
console.log(JSON.stringify(obj));

Works for me.

Filling the arrays works too.


Update

You imply that you are trying to add elements with non-numeric keys to arrays.

This is not valid. In particular, your desired output is not valid JSON. Arrays have only numeric keys, and they are not included in the JSON itself as they are implicitly, sequentially defined.

Arrays are a special type of Object, which handles numeric indexes for you.

var arr = [];   // Create array.
arr.push(1);    // There is now one element, with index 0 and value 1.
arr["txt"] = 2; // You tried to create a new element,
                // but didn't use .push and gave a non-numeric key.
                // This broke your array.

console.log(JSON.stringify(arr));
// Output: [1]

Live demo.

Long story short... don't do this. If you want an "associative array", stick with basic objects:

var obj    = {}; // Create object.
obj[0]     = 1;  // There is now one element, with key "0" and value 1.
obj["txt"] = 2;  // There is now a second element, with key "txt" and value 2.

console.log(JSON.stringify(arr));
// Output: {"0":1,"txt":2}

Live demo.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • @gman060692: Javascript does not have associative arrays. It has objects; a subset of objects are [numerically-indexed] arrays. If you're trying to push non-numeric keys into an array, then that is your problem. – Lightness Races in Orbit Aug 04 '11 at 15:25
  • the non numeric keys work fine when referencing the array items in my javascript code, Maybe firefox can handle associative arrays in javascript but the stringify function can't. Or is it that it's just creating layered objects instead of arrays. – gman060692 Aug 04 '11 at 15:36
  • @gman060692: No, arrays _do not have non-numeric keys_. You can retrieve the value of properties you set on your array's underlying object because that's how arrays are implemented and how Javascript works, but those non-numerically-keyed items are not part of the actual array data. It is _not_ valid to use arrays like this. Hopefully my updated answer clears things up for you. – Lightness Races in Orbit Aug 04 '11 at 15:37
  • @gman060692: A basic object, like in my final example. This is what you might call an "associative array", though this terminology cannot be strictly imported into a Javascript environment! That the enclosing symbols of your full object are `{` and `}` is no tomfoolery. – Lightness Races in Orbit Aug 04 '11 at 15:45
  • @gman060692: Not a problem. Welcome to Stack Overflow. May your visit be long and prosperous. – Lightness Races in Orbit Aug 04 '11 at 15:57
0
obj.toSource()

this will convert your array to source string.

Senad Meškin
  • 13,597
  • 4
  • 37
  • 55