当前位置:Gxlcms > JavaScript > 利用jquery如何从json中读取数据追加到html中

利用jquery如何从json中读取数据追加到html中

时间:2021-07-01 10:21:17 帮助过:2人阅读

JSON 格式

json 是 Ajax 中使用频率最高的数据格式,在浏览器和服务器中之间的通讯可离不开它。

JSON 格式说明

需要特别注意的是,在 JSON 中的属性名是需要使用引号引起来的。

1.下载安装jquery

可通过下面的方法引入在线版本的js:

  1. <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

参考安装文档://www.gxlcms.com/zt/jquerydown.htm

2.准备一个json格式的文件,后缀可以不是.json

例如下面是result.json的格式

  1. {
  2. "title":"【UI测试结果】-转转2017/1/23 14:47",
  3. "starttime":"2017/1/23 15:00 45",
  4. "endtime":"2017/1/23 15:01 42",
  5. "passcount":10,
  6. "failurecount":5,
  7. "resultinfo":[
  8. {
  9. "name":"发布",
  10. "moudle":"Publish",
  11. "pass":"true",
  12. "onecepass":"true",
  13. "log":"true"
  14. },
  15. {
  16. "name":"登录",
  17. "moudle":"Login",
  18. "pass":"false",
  19. "onecepass":"true",
  20. "log":"asserterrorlog",
  21. "failurereason":{
  22. "errorlog":"asserterror",
  23. "errorimg":"./登录.jpg"
  24. }
  25. }
  26. ]
  27. }

3.通过$.getJSON获得Json文件的数据

例如下面的例子:读取result.json文件的内容,存储到result变量中,结果是一个json格式

  1. $.getJSON('./result.json',function(result){}

4.通过【$('#元素id').after(html内容);】将html内容添加到定位到的元素后面

元素定位方式

  1. $("#id"):定位到id,
  2. $(“p"):定位到标签p,其他标签同理
  3. $(“.class”):定位到class

插入html内容位置:

  • append() - 在被选元素的结尾插入内容
  • prepend() - 在被选元素的开头插入内容
  • after() - 在被选元素之后插入内容
  • before() - 在被选元素之前插入内容

Json数据的操作

JSON对象[key]来读取内容:result['title'],或者用result.”title"

数组的对象值,可以通过$.each来获得数据:

$.each(JSON数组对象,function(遍历索引i,遍历对象){操作遍历的对象})

读取result.json,追加html的代码如下

(jquery需要写在<script>标签内)

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE> New Document </TITLE>
  5. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  6. <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  7. <script>
  8. $(document).ready(function(){
  9. //使用getJSON方法读取json数据,
  10. //注意:info.json可以是不同类型文件,只要其中的数据为json类型即可
  11. $.getJSON('./result.json',function(result){
  12. var html_title='';
  13. var html_resultinfo='';
  14. html_title += '<b>'+result["title"]+'</b>';
  15. $('#resultitle').after(html_title);
  16. $.each(result["resultinfo"],function(i,item){
  17. if(item["pass"]=="true") {
  18. html_resultinfo += '<tr><td>' + item['name'] + '</td>' +
  19. '<td>' + item['moudle'] + '</td>' +
  20. '<td>' + item["pass"] + '</td>' +
  21. '<td>' + item['onecepass'] + '</td>' +
  22. '<td id="' + item['moudle'] + '" class="collapsed" onclick="collapsedisplay(' + item['moudle'] + ')"><u style="color: blue;">展开</u></td></tr>';
  23. html_resultinfo +='<tr id="' + item['moudle'] + 'info" class="collapsedinfo" style="display:none"><td colspan="5">' + item['log'] + '</td></tr>';
  24. }
  25. $('#infotitle').after(html_resultinfo);//after方法:在每个匹配的元素之后插入内容。
  26. });
  27. });
  28. </script>
  29. </HEAD>
  30. <BODY>
  31. <div style="margin-top: 30px">
  32. <div style="font-size: 30px;text-align: center">
  33. <p id="resultitle" ></p>
  34. </div>
  35. </div>
  36. <div id="resultinfo" style="clear: both;padding-top: 30px">
  37. <table style="width: 1080px">
  38. <tr id="infotitle">
  39. <th style="width:360px">用例名称</th>
  40. <th style="width:200px">模块名称</th>
  41. <th style="width:180px">是否成功</th>
  42. <th style="width:180px">一次成功</th>
  43. <th style="width:160px">详情</th></tr>
  44. </table>
  45. </div>
  46. </div>
  47. </BODY>
  48. </HTML>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

人气教程排行