Jquery通过JSON字符串创建JSON对象
<html> <body> <h2>通过JSON字符串来创建对象</h3> <p> FirstName:<spanid="fname"></span><br/> LastName:<spanid="lname"></span><br/> </p> <scripttype="text/javascript"> //txt为字符串 vartxt='{"employees":['+ '{"firstName":"Bill","lastName":"Gates"},'+ '{"firstName":"George","lastName":"Bush"},'+ '{"firstName":"Thomas","lastName":"Carter"}]}'; //通过eval()方法 //varobj=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:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用:
在线JSON代码检验、检验、美化、格式化工具:
http://tools.jb51.net/code/json
JSON在线格式化工具:
http://tools.jb51.net/code/jsonformat
在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson
json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.jb51.net/code/jsoncodeformat
在线json压缩/转义工具:
http://tools.jb51.net/code/json_yasuo_trans
C语言风格/HTML/CSS/json代码格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json