我的json获取数据实例,直接运行就可以看到了

本文介绍了一个使用JavaScript解析JSON数据的具体实例。通过简单的HTML页面展示如何遍历并显示JSON对象中的数组信息,包括程序员、作家和音乐家等不同类型的人员数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开始一直用jq的ajax接收值也是字符但是没用过json数据。(所以一直都用切割的方法来做)听朋友们说JSon处理数据比较好,所以也加入了json这个行列了。快来看看吧。对AJAX比较接收处理数据比较好!力荐!...

转自:http://www.ok22.org/art_detail.aspx?id=136(可直接运行)

Html代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  4. <title>我的json获取数据实例,直接运行就可以看到了</title>  
  5. <meta name="Keywords" content="直接运行就可以看到了" />  
  6. <meta name="Description" content="是一个免费提供代码分享的一个网站,有最全的SEO资料,及Div+css,javascript,SEO学习网页前端设计" />  
  7. <head>  
  8. <script>  
  9. var people = {   
  10.     "programmers": [   
  11.     { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },   
  12.   {"firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },   
  13.   { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }   
  14.   ],   
  15.   "authors": [   
  16.   { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },   
  17.   { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },   
  18.   { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }   
  19.   ],   
  20.   "musicians": [   
  21.   { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },   
  22.   { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }   
  23.   ]    
  24. }   
  25. var lxr = {"AREA":[{"df":"请选择国籍","lg":"中国" },{"dsf":"江西","dperson":"九江"},{"dsf":"广东","dperson":"汕头"}]};   
  26. //alert(lxr.AREA[0].df+"--"+lxr.AREA[0].lg);   
  27. //for(i=1;i<lxr.AREA.length;i++)   
  28. //alert(lxr.AREA[i].dsf+"--"+lxr.AREA[i].dperson);   
  29. //alert(people.authors[1].genre);   
  30. //alert(people.length);   
  31. var str="";   
  32. var str2="";   
  33. </script>  
  34. </head>  
  35. <body>  
  36. <div id="info"></div>  
  37. <div id="area"></div>  
  38. </body>  
  39. </html>  
  40. <script>  
  41. for(j=0;j<people.programmers.length;j++)   
  42. {   
  43.     str+="programmers第"+j+1+"个"+people.programmers[j].firstName+"--"+people.programmers[j].lastName+"--"+people.programmers[j].email+"<br>"   
  44. }   
  45. for(j=0;j<people.authors.length;j++)   
  46. {   
  47.     str+="authors第"+j+1+"个"+people.authors[j].firstName+"---"+people.authors[j].lastName+"---"+people.authors[j].genre+"<br>"   
  48. }   
  49. for(j=0;j<people.musicians.length;j++)   
  50. {   
  51.     str+="musicians第"+j+1+"个"+people.musicians[j].firstName+"---"+people.musicians[j].lastName+"---"+people.musicians[j].instrument+"<br>"   
  52. }   
  53.   
  54. //alert(str);   
  55. str2+=lxr.AREA[0].df+"--"+lxr.AREA[0].lg+"<br>";   
  56. for(i=1;i<lxr.AREA.length;i++)   
  57.     str2+=lxr.AREA[i].dsf+"--"+lxr.AREA[i].dperson+"<br>";   
  58. document.getElementById("info").innerHTML=str;   
  59. document.getElementById("area").innerHTML=str2;   
  60. //alert(lxr.AREA[i].dsf+"--"+lxr.AREA[i].dperson);   
  61. //alert(people.authors[1].genre);   
  62. //alert(people.length);   
  63. </script>  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值