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?

1
  • Can you provide a more precise input/output example? There are certainly inconsistencies in yours. Commented Aug 4, 2011 at 15:09

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@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.
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: 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.
@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.
@gman060692: Not a problem. Welcome to Stack Overflow. May your visit be long and prosperous.
0
obj.toSource()

this will convert your array to source string.

1 Comment

I don't seem to have this function in Chrome?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.