<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> </head> <body> <script type="text/javascript"> var str = '{"id":1,"name":"YM","sex":"\u5973","age":0}'; var tmp = 'alert(1)'; eval(tmp); //将json字符串转换为对象1: var list = eval('('+str+')'); console.log(list); alert(list.sex); alert(list.name); //将json字符串转化为对象2 var obj = JSON.parse(str); console.log(obj); alert(obj.name); </script> </body> </html>
点击 "运行实例" 按钮查看在线实例