标签:
[转自]:JSON.parse()和JSON.stringify():http://blog.csdn.net/wangxiaohu__/article/details/7254598
1、parse用于从一个字符串中解析出json对象,如:
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 </head> 6 <body> 7 </body> 8 9 <script> 10 var str = ‘{"name":"jack","age":"23"}‘; //注意:单引号写在{}外,每个属性名都必须用双引号,否则会抛出异常。 11 var obj = JSON.parse(str); 12 for(var i in obj){ 13 console.log(‘obj-‘ + i + ‘:‘ + obj[i]); 14 } 15 </script> 16 17 </html>
2、stringify()用于从一个对象解析出字符串,如:
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 </head> 6 <body> 7 </body> 8 9 <script> 10 var person = {name:‘Rose‘,age:18} 11 var str = JSON.stringify(person); 12 alert(str); 13 </script> 14 15 </html>
标签:
原文地址:http://www.cnblogs.com/k11590823/p/4578791.html