JSON Getting Started learning notes sample code _json

Source: Internet
Author: User

JSON Introduction:

JSON (JavaScript Object notation) is a lightweight data interchange format. Easy for people to read and write.
JSON official website http://www.json.org/
Json.NET Download Address http://json.codeplex.com/releases/view/37810

Demo:

  

Describe:
After clicking the Get button, you will request the page, get the JSON data, populate the table
HTML code:

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 ">
<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"). html (row);
}
</script>
<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>

Background code:
Copy Code code 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 = "book", Price = "=", 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 Points:
1.json officially provides a DLL class library for. NET operations, which jsonconvert.serializeobject. NET objects into JSON.
2.Javascript reads the JSON object var product = eval (callbackmsg).
3.Javascript read 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.