当前位置: 编程技术>WEB前端
Jquery通过JSON字符串创建JSON对象
来源: 互联网 发布时间:2014-08-25
本文导语: 通过 JSON 字符串来创建对象 First Name: Last Name: //txt为字符串 var txt = '{"employees":[' + '{"firstName":"Bill","lastName":"Gates" },' + '{"firstName":"George","lastName":"Bush" },' + '{"firstName":"Thomas","lastName":"Carter" }]}'; //通过eval()方法 /...
通过 JSON 字符串来创建对象First Name:
//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
Last Name: