json文件是一種輕量級的數據交互格式。一般在jquery中使用getJSON()方法讀取。
$.getJSON(url,[data],[callback])
url:加載的頁面地址
data: 可選項,發送到服務器的數據,格式是key/value
callback:可選項,加載成功后執行的回調函數
1.首先建一個JSON格式的文件userinfo.json 保存用戶信息。如下:
[ { "name":"張國立", "sex":"男", "email":"[email protected]" }, { "name":"張鐵林", "sex":"男", "email":"[email protected]" }, { "name":"鄧婕", "sex":"女", "email":"[email protected]" } ]
2.其次建一個頁面用于獲取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獲取數據</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"; //存儲數據的變量 $jsontip.empty(); //清空內容 $.each(data, function (infoIndex, info){ strHtml += "姓名:" + info["name"] + "<br>"; strHtml += "性別:" + info["sex"] + "<br>"; strHtml += "郵箱:" + info["email"] + "<br>"; strHtml += "<hr>" }) $jsontip.html(strHtml); //顯示處理后的數據 }) }) })</script> </head> <body> <div id="divframe"> <div class="loadTitle"> <input type="button" value="獲取數據" id="btn"/> </div> <div id="jsonTip"> </div> </div> </body> </html>
這里武林網小編繼續為大家分享一下,如果想加載后自動加載內容的寫法(圖片與超鏈接)
da.json
[{ "img": "http://files.VeVB.COm/image/http.gif", "url":"http://www.companysz.com/1" },{ "img": "http://files.VeVB.COm/image/jbzj.gif", "url":"http://www.companysz.com/2" },{ "img": "http://files.VeVB.COm/image/tengxunyun.jpg", "url":"http://www.companysz.com/3" }]
通過ajax獲取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>通過ajax獲取json數據的實現代碼</title><script type="text/javascript" src="http://www.companysz.com/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.companysz.com/jslib/jquery/jquery.min.js"></script></head><body><div id="ok"></div><script>$(function(){ $.getJSON("da.json",function(data){ var $jsontip = $("#ok"); var strHtml = "";//存儲數據的變量 $jsontip.empty();//清空內容 $.each(data,function(infoIndex,info){ strHtml += "<a href='" + info["url"] + "' target='_blank'><img src='" + info["img"] + "' /></a>";}) $jsontip.html(strHtml);//顯示處理后的數據 }) }) </script></body></html>
這樣效果就出來了如下圖所示就說明代碼沒問題
|
新聞熱點
疑難解答