收藏本站 收藏本站
积木网首页 - 软件测试 - 常用手册 - 站长工具 - 技术社区
首页 > JavaScript > jQuery > 正文

首页 - PHP - 数据库 - 操作系统 - 游戏开发 - JS - Android - MySql - Redis - MongoDB - Win8 - Shell编程 - DOS命令 - jQuery - CSS样式 - Python - Perl

Access - Oracle - DB2 - SQLServer - MsSql2008 - MsSql2005 - Sqlite - PostgreSQL - node.js - extjs - JavaScript vbs - Powershell - Ruby

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

JSON 格式

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

JSON 格式说明

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

1.下载安装jquery

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

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

参考安装文档:http://www.gimoo.net/zt/jquerydown.htm

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

例如下面是result.json的格式

{
 "title":"【UI测试结果】-转转2017/1/23 14:47",
 "starttime":"2017/1/23 15:00 45",
 "endtime":"2017/1/23 15:01 42",
 "passcount":10,
 "failurecount":5,
 "resultinfo":[
 {
 "name":"发布",
 "moudle":"Publish",
 "pass":"true",
 "onecepass":"true",
 "log":"true"
 },
 {
 "name":"登录",
 "moudle":"Login",
 "pass":"false",
 "onecepass":"true",
 "log":"asserterrorlog",
 "failurereason":{
  "errorlog":"asserterror",
  "errorimg":"./登录.jpg"
  }
 }
 ]
}

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

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

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

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

元素定位方式

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

插入html内容位置:

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

Json数据的操作

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

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

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

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

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
//使用getJSON方法读取json数据,
//注意:info.json可以是不同类型文件,只要其中的数据为json类型即可
 $.getJSON('./result.json',function(result){
 var html_title='';
 var html_resultinfo='';
 
 html_title += '<b>'+result["title"]+'</b>';
 $('#resultitle').after(html_title);
 $.each(result["resultinfo"],function(i,item){
 if(item["pass"]=="true") {
 html_resultinfo += '<tr><td>' + item['name'] + '</td>' +
 '<td>' + item['moudle'] + '</td>' +
 '<td>' + item["pass"] + '</td>' +
 '<td>' + item['onecepass'] + '</td>' +
 '<td id="' + item['moudle'] + '" class="collapsed" onclick="collapsedisplay(' + item['moudle'] + ')"><u style="color: blue;">展开</u></td></tr>';
 html_resultinfo +='<tr id="' + item['moudle'] + 'info" class="collapsedinfo" style="display:none"><td colspan="5">' + item['log'] + '</td></tr>';
 }
$('#infotitle').after(html_resultinfo);//after方法:在每个匹配的元素之后插入内容。
 });
});
 
</script>
</HEAD>

<BODY>
<div style="margin-top: 30px">
 <div style="font-size: 30px;text-align: center">
 <p id="resultitle" ></p>
 </div>
 </div>
 <div id="resultinfo" style="clear: both;padding-top: 30px">
 <table style="width: 1080px">
 <tr id="infotitle">
 <th style="width:360px">用例名称</th>
 <th style="width:200px">模块名称</th>
 <th style="width:180px">是否成功</th>
 <th style="width:180px">一次成功</th>
 <th style="width:160px">详情</th></tr>
 </table>
 </div>

</div>
</BODY>
</HTML>

总结

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

将jquery.qqFace.js表情转换成微信的字符码
jquery.qqFace.js使用方法引用scriptsrc="~/Content/qqFace/js/jquery.qqFace.jsv=3"/scriptscriptsrc="~/Content/qqFace/js/jquery-browser.js"/scripthtmldivstyle="width:50px;height:30px;margin-top:3px;

jQuery中的ajax async同步和异步详解
项目中有这样一个需求,使用ajax加载数据返回页面并赋值,然后前端取出该值这其中涉及到代码的顺序问题,有时后台还未返回数据,但已执行后面代

利用jQuery实现漂亮的圆形进度条倒计时插件
jQueryFinalCountdown是一款时尚的圆形进度条样式的jQuery倒计时插件。该倒计时插件可以显示倒计时的秒、分钟、小时和天数。它采用圆形进度条来作为倒计

本周排行

更新排行

强悍的草根IT技术社区,这里应该有您想要的!
Copyright © 2010 Gimoo.Net. All Rights Rreserved  京ICP备05050695号