JSON.stringify serializes to [[]]

后端 未结 4 2040
粉色の甜心
粉色の甜心 2021-01-25 18:39

If I create a JavaScript object like:

var lst = [];
var row = [];
row.Col1 = \'val1\';
row.Col2 = \'val2\'; 
lst.push(row);

And then convert it

4条回答
  •  遥遥无期
    2021-01-25 19:25

    Since an array is a datatype in JSON, actual instances of Array are stringified differently than other object types.

    If a JavaScript Array instance got stringified with its non-numeric keys intact, it couldn't be represented by the [ ... ] JSON array syntax.

    For instance, [ "Col1": "val1"] would be invalid, because JSON arrays can't have explicit keys.
    {"Col1": "val1"} would be valid - but it's not an array.

    And you certainly can't mix'n'match and get { "Col1": "val1", 1, 2, 3 ] or something.

    By the way, this works fine:

    var lst = [];
    var row = {};
    row.Col1 = 'val1';
    row.Col2 = 'val2'; 
    lst.push(row);
    alert(JSON.stringify(lst));​
    

提交回复
热议问题