JavaScript中如何反序列化json字符串呢?
有如下两种方法:
(1)使用万能的eval
- var jsonText = '{"name":"acwong","age":23,"address":{"province":"GuangDong","city":"GuangZhou"},"friends":["bc","cc","dc"],"blog":"http://acwong.org"}';
- var person = eval("("+jsonText+")");
运行结果:
(2)使用JSON.parse
- var jsonText = '{"name":"acwong","age":23,"address":{"province":"GuangDong","city":"GuangZhou"},"friends":["bc","cc","dc"],"blog":"http://acwong.org"}';
- var person = JSON.parse(jsonText);
- console.log(person);
运行结果:
JSON对象结构如下:
可以看出它有两个方法,分别是: parse()用于反序列化 和stringify()用于序列化为字符串
参考:http://segmentfault.com/blog/acwong/1190000002580585
或
http://acwong.org/2015/03/05/json-notes/
json-lib 序列化和反序列化:http://hw1287789687.iteye.com/blog/1993318
http://hw1287789687.iteye.com/blog/1997956