<html><body><h2>通過 JSON 字符串來創(chuàng)建對象</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>
PS:關(guān)于json操作,這里再為大家推薦幾款比較實用的json在線工具供大家參考使用:
在線JSON代碼檢驗、檢驗、美化、格式化工具:
http://tools.VeVB.COm/code/json
JSON在線格式化工具:
http://tools.VeVB.COm/code/jsonformat
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.VeVB.COm/code/xmljson
json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.VeVB.COm/code/jsoncodeformat
在線json壓縮/轉(zhuǎn)義工具:
http://tools.VeVB.COm/code/json_yasuo_trans
C語言風格/HTML/CSS/json代碼格式化美化工具:
http://tools.VeVB.COm/code/ccode_html_css_json
新聞熱點
疑難解答