Jquery通过JSON字符串创建JSON对象
程序员文章站
2023-10-17 10:31:35
通过 json 字符串来创建对象
first name...
<html> <body> <h2>通过 json 字符串来创建对象</h3> <p> first name: <span id="fname"></span><br /> last name: <span id="lname"></span><br /> </p> <script type="text/javascript"> //txt为字符串 var txt = '{"employees":[' + '{"firstname":"bill","lastname":"gates" },' + '{"firstname":"george","lastname":"bush" },' + '{"firstname":"thomas","lastname":"carter" }]}'; //通过eval()方法 //var obj = eval ("(" + txt + ")"); //通过json解析 //obj = jquery.parsejson(txt); obj = json.parse(txt); document.getelementbyid("fname").innerhtml=obj.employees[1].firstname document.getelementbyid("lname").innerhtml=obj.employees[1].lastname </script> </body> </html>
上一篇: 虚拟内存