如何把json数组反序列化

2024-12-28 09:57:25
推荐回答(1个)
回答1:

//序列化JSON.stringify(json)
var jsonArray = [];
jsonArray.push({a:1,b:2});
jsonArray.push({a:2,b:3});
console.log('jsonArray:'+jsonArray);

console.log('jsonArray1:'+JSON.stringify(jsonArray));
//反序列化JSON.parse(json)
var a = JSON.stringify(jsonArray);

var b = JSON.parse(a);
b..push({a:3,b:4});
console.log('b:'+b);
console.log('b1:'+JSON.stringify(b));
上面的代码执行后的输出结果:
[2016-05-21 11:59:52.764] [INFO] console - jsonArray:[object Object],[object Object]

[2016-05-21 11:59:52.765] [INFO] console - jsonArray1:[{"a":1,"b":2},{"a":2,"b":3}]
[2016-05-21 11:59:52.765] [INFO] console - b:[object Object],[object Object],[object Object]
[2016-05-21 11:59:52.766] [INFO] console - b1:[{"a":1,"b":2},{"a":2,"b":3},{"a":3,"b":4}]