JSON introduction:
JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy to read and write.
JSON Official WebsiteHttp://www.json.org/
Json.netHttp://json.codeplex.com/releases/view/37810
Demo:
Description:
Click the get button to retrieve JSON data on the request page and fill it in the table.
HtmlCode:
Copy code The Code is as follows: <! 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 runat = "server">
<Title> </title>
<SCRIPT type = "text/JScript" src = "../jquery/jquery-1.4.2.min.js"> </SCRIPT>
<Script language = "JavaScript" type = "text/JavaScript">
$ (Function (){
$ ("# Button1"). Click (function (){
$. Get ("json1.aspx? M = m ", null, function (callbackmsg ){
VaR Product = eval (callbackmsg );
Showproduct (product)
})
});
})
Function showproduct (callbackmsg ){
VaR ROW = '';
For (var u in callbackmsg ){
VaR Len = callbackmsg [u]. length;
Row + = '<tr> ';
Row + = '<TD>' + callbackmsg [u]. Name + '</TD> ';
Row + = '<TD>' + callbackmsg [u]. Price + '</a> </TD> ';
Row + = '<TD>' + callbackmsg [u]. Size + '</TD> ';
Row + = '</tr> ';
}
$ ("# Body" rows .html (ROW );
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Input id = "button1" type = "button" value = "get"/>
</Div>
<Table>
<Thead>
<Tr>
<TH>
Name
</Th>
<TH>
Price
</Th>
<TH>
Size
</Th>
</Tr>
</Thead>
<Tbody id = "body">
</Tbody>
</Table>
</Form>
</Body>
</Html>
Background code: Copy code The Code is as follows: public partial class json1: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (httpcontext. Current. Request. querystring ["M"]! = NULL)
{
List <product> Products = new list <product> ();
Products. Add (New Product () {name = "Notebook", price = "4.2", size = "30x50 "});
Products. Add (New Product () {name = "ruler", price = "1.2", size = "30x50 "});
Products. Add (New Product () {name = "", price = "36", size = "30x50 "});
Products. Add (New Product () {name = "pen", price = "6.0", size = "30x50 "});
Products. Add (New Product () {name = "pencil", price = "2.2", size = "30x50 "});
String JSON = jsonconvert. serializeobject (products );
Response. Write (JSON );
Response. End ();
}
}
}
Public class product
{
Public string name {Get; set ;}
Public String size {Get; set ;}
Public String price {Get; set ;}
}
Knowledge point:
1. JSON officially provides the DLL class library for. Net operations. jsonconvert. serializeobject serializes. Net objects into JSON.
2. Javascript reads the JSON object var Product = eval (callbackmsg ).
3. Javascript reads the JSON value callbackmsg [u]. Name.