我不确定,但我认为您可能会对数组如何在JSON中序列化感到惊讶。让我们隔离问题。考虑以下代码:
var display = Array();display[0] = "none";display[1] = "block";display[2] = "none";console.log( JSON.stringify(display) );
这将打印:
["none","block","none"]
这就是JSON实际上序列化数组的方式。但是,您想看到的是这样的:
{"0":"none","1":"block","2":"none"}要获得此格式,您要序列化对象,而不是数组。因此,让我们像这样重写上面的代码:
var display2 = {};display2["0"] = "none";display2["1"] = "block";display2["2"] = "none";console.log( JSON.stringify(display2) );这将以您想要的格式打印。



