无法序列化javascript数组

时间:2013-12-10 07:30:31

标签: javascript arrays serialization

我有一个类似于此的关联数组(不是实际代码):

sort_order['14682007'][0] = "1"
sort_order['14682007'][1] = "0"
sort_order['82501220'][0] = "2"
sort_order['82501220'][1] = "1"
sort_order['82501220'][2] = "0"

我想用这种方式序列化这个数组:

sort_order = JSON.stringify(sort_order);

但它只返回一个空字符串。 Colud有人帮助我吗?

1 个答案:

答案 0 :(得分:3)

确保正确声明变量,例如:

    var sort_order = {};
    sort_order['82501220'] = [];
    sort_order['14682007'] = [];
    sort_order['14682007'][0] = "1";
    sort_order['14682007'][1] = "0";
    sort_order['82501220'][0] = "2";
    sort_order['82501220'][1] = "1";
    sort_order['82501220'][2] = "0";

    console.log(JSON.stringify(sort_order));

注意:如果在这种情况下将sort_order声明为数组,我认为你将拥有一个非常长的稀疏数组。