简单教程
提交运行
代码编辑器:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>简单教程( www.twle.cn )</title> </head> <body> <h2>从 JSON 字符串中创建对象</h2> <p> 网站名: <span id="name"></span><br> 网站地址: <span id="url"></span><br> </p> <script> var json_str = '{ "sites" : [' + '{ "name":"简单教程" , "url":"www.twle.cn" },' + '{ "name":"阿里巴巴" , "url":"www.taobao.com" },' + '{ "name":"腾讯" , "url":"www.qq.com" }]}'; obj = JSON.parse(json_str); document.getElementById("name").innerHTML=obj.sites[0].name document.getElementById("url").innerHTML=obj.sites[0].url </script> </body> </html>
运行结果: