Sample code for reading JSON files in jquery

Source: Internet
Author: User

JSON files are a lightweight form of data interaction. Generally read in jquery using the Getjson () method, the specific sample code is as follows, interested friends can refer to the next ha, hoping to help your JSON file is a lightweight data interchange format. Typically read in jquery using the Getjson () method.
Copy CodeThe code is 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 loading succeeded
1. First build a JSON-formatted file Userinfo.json Save the user information. As follows:
Copy CodeThe code is as follows:
[
{
"Name": "Zhang National",
"Sex": "Male",
"Email": "[email protected]"
},
{
"Name": "Zhang Tielin",
"Sex": "Male",
"Email": "[email protected]"
},
{
"Name": "Deng Jie takes",
"Sex": "Female",
"Email": "[email protected]"
}
]
2. Next, a page is used to obtain the user information data in the JSON file and display
Copy CodeThe code is 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";//variables that store data
$jsontip. Empty ();//Clear Contents
$.each (Data,function (infoindex,info) {
strHTML + = "Name:" +info["name"]+ "<br>";
strHTML + = "Gender:" +info["Sex"]+ "<br>";
strHTML + = "e-mail:" +info["email"]+ "<br>";
strHTML + = "})
$jsontip. HTML (strhtml);//display of processed data
})
})

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

Sample code for reading JSON files in jquery

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.