Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the steps to read json with jQuery

php中世界最好的语言
Release: 2018-04-23 14:24:56
Original
1636 people have browsed it

This time I will bring you a detailed explanation of the steps for jQuery to read json. What are the precautions for jQuery to read json? The following is a practical case, let's take a look.

json file is a lightweight data interaction format. Generally, it is read using the getJSON() method in jquery.

$.getJSON(url,[data],[callback])
Copy after login

url: Loaded page address
data: Optional, the data sent to the server, the format is key/value
callback: Optional, the callback function executed after successful loading
1. First create a JSON format file userinfo.json to save user information. As follows:

[ 
{ 
"name":"张国立", 
"sex":"男", 
"email":"zhangguoli@123.com" 
}, 
{ 
"name":"张铁林", 
"sex":"男", 
"email":"zhangtieli@123.com" 
}, 
{ 
"name":"邓婕", 
"sex":"女", 
"email":"zhenjie@123.com" 
} 
]
Copy after login

2. Secondly, create a page to obtain the user information data in the JSON file and display

<!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"> 
#pframe{ 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> 
<p id="pframe"> 
<p class="loadTitle"> 
<input type="button" value="获取数据" id="btn"/> 
</p> 
<p id="jsonTip"> 
</p> 
</p> 
</body> 
</html>
Copy after login

Here, the editor of Script House will continue to share it with you. If you want to load AfterAutomatic loadingHow to write content (pictures and hyperlinks)

da.json

[
{ "img": "http://files.jb51.net/image/http.gif", "url":"http://www.jb51.net/1" },
{ "img": "http://files.jb51.net/image/jbzj.gif", "url":"http://www.jb51.net/2" },
{ "img": "http://files.jb51.net/image/tengxunyun.jpg", "url":"http://www.jb51.net/3" }
]
Copy after login

Implementation code for obtaining json data through ajax

<!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="//www.jb51.net/jslib/jquery/jquery.min.js"></script>
</head>
<body>
<p id="ok"></p>
<script>
$(function () {
    $.ajax({
      type: "POST",
      dataType: "json",
      url: "da.json",
      success: function (result) {
        var str = "";
				$.each(result,function(index,obj){				
				str += "<a href=&#39;" + obj["url"] + "&#39; target=&#39;_blank&#39;><img src=&#39;" + obj["img"] + "&#39; /></a>";					
				});
        $("#ok").append(str);
      }
    });
});
</script>
</body>
</html>
Copy after login

Passed $.getJSON gets the json code

<!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="//www.jb51.net/jslib/jquery/jquery.min.js"></script>
</head>
<body>
<p id="ok"></p>
<script>
$(function(){ 
$.getJSON("da.json",function(data){ 
var $jsontip = $("#ok"); 
var strHtml = "";//存储数据的变量 
$jsontip.empty();//清空内容 
$.each(data,function(infoIndex,info){
	strHtml += "<a href=&#39;" + info["url"] + "&#39; target=&#39;_blank&#39;><img src=&#39;" + info["img"] + "&#39; /></a>";
}) 
$jsontip.html(strHtml);//显示处理后的数据 
}) 
}) 
</script>
</body>
</html>
Copy after login

The effect will come out as shown in the figure below, which means the code is OK

I believe you have read the case in this article After mastering the method, please pay attention to other related articles on the php Chinese website for more exciting content!

Recommended reading:

Jquery LigerUI implementation of file upload steps detailed explanation

How jQuery reads XML file content

The above is the detailed content of Detailed explanation of the steps to read json with jQuery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!