If I create a JavaScript object like:
var lst = [];
var row = [];
row.Col1 = 'val1';
row.Col2 = 'val2';
lst.push(row);
And then convert it to a string:
JSON.stringify(lst);
The result is an object containing an empty object:
[[]]
I would expect it to serialize like:
[[Col1 : 'val1', Col2: 'val2']]
Why do the inner objects properties not serialize?
Because
rowis an array, not an object. Change it to:This creates an object literal. Your code will then result in an array of objects (containing a single object):
Update
To see what really happens, you can look at json2.js on GitHub. This is a (heavily reduced) snippet from the
strfunction (called byJSON.stringify):Notice that arrays are iterated over with a normal
forloop, which only enumerates the array elements. Objects are iterated with afor...inloop, with ahasOwnPropertytest to make sure the proeprty actually belongs to this object.