<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>