JSON learning notes sample code

Source: Internet
Author: User

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.

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.