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.