JSON.stringify serializes to [[]]

后端 未结 4 2034
粉色の甜心
粉色の甜心 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:05

    You want row to be a dictionary, not a vector. Define it like this:

    var row = {};
    
    0 讨论(0)
  • 2021-01-25 19:09

    Because row is an array, not an object. Change it to:

    var row = {};  
    

    This creates an object literal. Your code will then result in an array of objects (containing a single object):

    [{"Col1":"val1","Col2":"val2"}]
    

    Update

    To see what really happens, you can look at json2.js on GitHub. This is a (heavily reduced) snippet from the str function (called by JSON.stringify):

    if (Object.prototype.toString.apply(value) === '[object Array]') {
        //...
        length = value.length;
        for (i = 0; i < length; i += 1) {
            partial[i] = str(i, value) || 'null';
        }
        //...
    }
    //...
    for (k in value) {
        if (Object.prototype.hasOwnProperty.call(value, k)) {
            //...
        }
        //...
    }
    //...
    

    Notice that arrays are iterated over with a normal for loop, which only enumerates the array elements. Objects are iterated with a for...in loop, with a hasOwnProperty test to make sure the proeprty actually belongs to this object.

    0 讨论(0)
  • 2021-01-25 19:13

    You use your inner array like an object, so make it an object instead of an array.

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

    or use it as an array

    var lst = [];
    var row = {};
    row.push( 'val1' );
    row.push( 'val2' );
    lst.push(row);
    
    0 讨论(0)
  • 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));​
    
    0 讨论(0)
提交回复
热议问题