JavaScript中JSON字符串和JSON对象相互转化
JSON字符串转化为JSON对象的2种方式
一、使用函数eval
1 var personsstr = '[{"Name":"zhangsan","Age":26},{"Name":"lisi","Age":26}]'; 2 var personsjson = eval(personsstr);
转化后结果:
二、使用JSON.parse
1 var personsstr = '[{"Name":"zhangsan","Age":26},{"Name":"lisi","Age":26}]'; 2 var personsjson = JSON.parse(personsstr);
JSON对象转化为JSON字符串
1 var personsjson = [{ "Name": "zhangsan", "Age": 26 }, { "Name": "lisi", "Age": 26}]; 2 var personsstr2 = JSON.stringify(personsjson);
转化后结果: