欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery中讀取json文件示例代碼

 更新時間:2013年05月10日 15:39:13   投稿:whsnow  
json文件是一種輕量級的數(shù)據(jù)交互格式。一般在jquery中使用getJSON()方法讀取,具體示例代碼如下,感興趣的朋友可以參考下哈,希望可以幫助到你

json文件是一種輕量級的數(shù)據(jù)交互格式。一般在jquery中使用getJSON()方法讀取。

$.getJSON(url,[data],[callback])

url:加載的頁面地址
data: 可選項(xiàng),發(fā)送到服務(wù)器的數(shù)據(jù),格式是key/value
callback:可選項(xiàng),加載成功后執(zhí)行的回調(diào)函數(shù)
1.首先建一個JSON格式的文件userinfo.json 保存用戶信息。如下:

[ 
{ 
"name":"張國立", 
"sex":"男", 
"email":"zhangguoli@123.com" 
}, 
{ 
"name":"張鐵林", 
"sex":"男", 
"email":"zhangtieli@123.com" 
}, 
{ 
"name":"鄧婕", 
"sex":"女", 
"email":"zhenjie@123.com" 
} 
] 

2.其次建一個頁面用于獲取JSON文件里的用戶信息數(shù)據(jù),并顯示

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>getJSON獲取數(shù)據(jù)</title> 
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script> 
<style type="text/css"> 
#divframe{ border:1px solid #999; width:500px; margin:0 auto;} 
.loadTitle{ background:#CCC; height:30px;} 
</style> 
< script type = "text/javascript" > 
$(function (){
  $("#btn").click(function ()  {
    $.getJSON("js/userinfo.json", function (data){
      var $jsontip = $("#jsonTip");
      var strHtml = "123";
      //存儲數(shù)據(jù)的變量 
      $jsontip.empty();
      //清空內(nèi)容 
      $.each(data, function (infoIndex, info){
        strHtml += "姓名:" + info["name"] + "<br>";
        strHtml += "性別:" + info["sex"] + "<br>";
        strHtml += "郵箱:" + info["email"] + "<br>";
        strHtml += "<hr>" 
      }) 
	  $jsontip.html(strHtml);
      //顯示處理后的數(shù)據(jù) 
    }) 
  }) 
})
</script> 
</head> 
<body> 
<div id="divframe"> 
<div class="loadTitle"> 
<input type="button" value="獲取數(shù)據(jù)" id="btn"/> 
</div> 
<div id="jsonTip"> 
</div> 
</div> 
</body> 
</html> 

這里腳本之家小編繼續(xù)為大家分享一下,如果想加載后自動加載內(nèi)容的寫法(圖片與超鏈接)

da.json

[
{ "img": "http://img.jbzj.com/image/http.gif", "url":"http://www.dbjr.com.cn/1" },
{ "img": "http://img.jbzj.com/image/jbzj.gif", "url":"http://www.dbjr.com.cn/2" },
{ "img": "http://img.jbzj.com/image/tengxunyun.jpg", "url":"http://www.dbjr.com.cn/3" }
]

通過ajax獲取json數(shù)據(jù)的實(shí)現(xiàn)代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>通過ajax獲取json數(shù)據(jù)的實(shí)現(xiàn)代碼</title>
<script type="text/javascript" src="http://www.dbjr.com.cn/jslib/jquery/jquery.min.js"></script>
</head>
<body>
<div id="ok"></div>
<script>
$(function () {
    $.ajax({
      type: "POST",
      dataType: "json",
      url: "da.json",
      success: function (result) {
        var str = "";
				$.each(result,function(index,obj){				
				str += "<a href='" + obj["url"] + "' target='_blank'><img src='" + obj["img"] + "' /></a>";					
				});
        $("#ok").append(str);
      }
    });
});
</script>
</body>
</html>

通過$.getJSON獲取json的代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>通過$.getJSON獲取json的代碼</title>
<script type="text/javascript" src="http://www.dbjr.com.cn/jslib/jquery/jquery.min.js"></script>
</head>
<body>
<div id="ok"></div>
<script>
$(function(){ 
$.getJSON("da.json",function(data){ 
var $jsontip = $("#ok"); 
var strHtml = "";//存儲數(shù)據(jù)的變量 
$jsontip.empty();//清空內(nèi)容 
$.each(data,function(infoIndex,info){
	strHtml += "<a href='" + info["url"] + "' target='_blank'><img src='" + info["img"] + "' /></a>";
}) 
$jsontip.html(strHtml);//顯示處理后的數(shù)據(jù) 
}) 
}) 
</script>
</body>
</html>

這樣效果就出來了如下圖所示就說明代碼沒問題

相關(guān)文章

最新評論