Read the JSON file sample code in jquery _jquery

Source: Internet
Author: User
A JSON file is a lightweight data interchange format. Typically read in jquery using the Getjson () method.
Copy Code code as follows:

$.getjson (Url,[data],[callback])

URL: Loaded page address
Data: Optional, sent to the server, the format is Key/value
Callback: Optional, callback function executed after successful loading
1. First create a JSON-formatted file Userinfo.json save user information. As follows:
Copy Code code as follows:

[
{
"Name": "Zhang State",
"Sex": "Male",
"Email": "zhangguoli@123.com"
},
{
"Name": "Zhang Tielin",
"Sex": "Male",
"Email": "zhangtieli@123.com"
},
{
"Name": "Deng Jie takes",
"Sex": "Female",
"Email": "zhenjie@123.com"
}
]

2. Then build a page to get the user information data in the JSON file, and show
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>getjson Get Data </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";//the variable that stores the data
$jsontip. Empty ();//empty content
$.each (Data,function (infoindex,info) {
strHTML + + "Name:" +info["name"]+ "<br>";
strHTML + + "Sex:" +info["Sex"]+ "<br>";
strHTML + + "e-mail:" +info["email"]+ "<br>";
strHTML = "})
$jsontip. HTML (strhtml);//Display processed data
})
})

})
</script>
<body>
<div id= "Divframe" >
<div class= "Loadtitle" >
<input type= "button" value= "Fetch data" id= "BTN"/>
</div>
<div id= "Jsontip" >
</div>
</div>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.