Jquery reads the two example codes of the json file (1/2)

Source: Internet
Author: User

The article provides two examples of jquery webpage reading special effects on files for free. These codes are very practical. They are the second complete jquery code for reading json files.

Json file:

[
{
"Name": "xujun ",
"Sex": "male ",
"Home": "nanjing"
},
{
"Name": "jack ",
"Sex": "male ",
"Home": "beijing"
}
]

Html file:
<! Doctype html public "-// w3c // dtd html 4.01 transitional // en" "http: // wwwbKjia. c0m/tr/html4/loose. dtd">
<Html>
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<Title> insert title here </title>
<Script src = ".../jquery. js"> </script>
<Script>
$ (Document). ready (function (){
$. Getjson ('jsondata. json? Id', function (data ){
// Traverse every entry in json

$. Each (data, function (entryindex, entry ){
Var html = '<tr> ';
Html + = '<td>' + entry ['name'] + '</td> ';
Html + = '<td>' + entry ['sex'] + '</td> ';
Html + = '<td>' + entry ['home'] + '</td> ';
Html + = '</tr> ';
$ ('# Title'). after (html );

});
});
});
</Script>
</Head>
<Body>
<Table>
<Tr id = "title">
<Th> name </th>
<Th> gender </th>
<Th> Home address </th>
</Tr>
</Table>
</Body>
</Html>

Instance 2

<! Doctype html public "-// w3c // dtd html 4.01 // en" "http://www.w3.org/tr/html4/strict.dtd">
<Html>
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = iso-8859-1">
<Title> json document </title>
<Script type = "text/Webpage effects" src = "lib/jquery/jquery-1.3.2.js"> </script>
<Script type = "text/javascript">
$ (Function (){
$ ("# Button"). click (function (){
$. Getjson (B. json, function (data) {// The getjson method obtains the code of the json file. The callback function parameter data is the json code.
$ ("# Json"). empty (); // clear the content of the div tag with "json" id
$. Each (data, function (entryindex, entry) {// The each method traverses objects in the json file. entryindex is the current index and entry is the current item.
Var html = "<div class = entry> ";
Html + = "

Html + = "<div class = part>" + entry [part] + "</div> ";
Html + = "<div class = definition>" + entry [definition];
If (entry [quote]) {
Html + = "<div class = quote> ";
$. Each (entry [quote], function (quoteindex, line ){
Html + = "<p>" + line + "</p> ";
});
Html + = "</div> ";
}
Html + = "</div> ";
Html + = "</div> ";
$ ("# Json"). append (html );
});
});
});
});
</Script>
</Head>
<Body>
<Div id = "button"> <B> get json document </B> </div>
<Div id = "json"> </div>
</Body>
</Html>

1 2

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.