JSON对象与JSON字符串相互转换
<!DOCTYPE html><html> <head> <title>JSON测试 </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <script>// 根据JSON对象的name获取valuevar jsonObj = {"name":"123"};// JSON对象console.log(jsonObj.name);var jsonStr = '{"name":"123"}';var jsonObj2 = eval("("+jsonStr+")");console.log("hehe:"+jsonObj2.name);// JSON字符串到JSON对象方法二//var jsonStr3 = '{name:"123"}';var jsonObj3 = JSON.parse(jsonStr);console.log("hello:"+jsonObj3.name);alert(jsonObj3.name);var jsonStr2 = JSON.stringify(jsonObj);console.log(jsonStr2);</script> </body> </html>