Javascript中eval解析的json的几种用法
eval解析json字符串可用的三种方式都可以实现。。。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <script type="text/javascript"> //json以数组方式存在 var data = "[{\"book1\":\"javabook1\",\"book2\":\"javabook2\"}]"; var json = eval("("+data+")"); document.write(json[0].book1); document.write("<br/>"); document.write(json[0].book2); </script> <br/><br/> <script type="text/javascript"> //json中只有一个对象,且内双引号用的转义字符 var data1 = "{\"book1\":\"javabook1\",\"book2\":\"javabook2\"}"; var json1 = eval("("+data1+")"); document.write(json1.book1); document.write("<br/>"); document.write(json1.book2); </script> <br/><br/> <script type="text/javascript"> //json中只有一个对象,且双引号可以包单引号 var data2 = "{'book1':'javabook1','book2':'javabook2'}"; var json2 = eval("("+data2+")"); document.write(json2.book1); document.write("<br/>"); document.write(json2.book2); </script> </body> </html>