Json介绍:
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。
json 官方网站 http://www.json.org/
json.net 下载地址 http://json.codeplex.com/releases/view/37810
Demo:
描述:
点击获取按钮后,将请求页面,获取json数据,填充到表格
html代码:
后台代码:
public partial class json1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.Request.QueryString["m"] != null)
{
List
Products = new List();
Products.Add(new Product() { Name = "笔记本", Price = "4.2", Size = "30x50" });
Products.Add(new Product() { Name = "尺子", Price = "1.2", Size = "30x50" });
Products.Add(new Product() { Name = "书", Price = "36", Size = "30x50" });
Products.Add(new Product() { Name = "钢笔", Price = "6.0", Size = "30x50" });
Products.Add(new Product() { Name = "铅笔", 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; }
}
知识点:
1.json官方提供了用于.net操作的dll类库,JsonConvert.SerializeObject将.net对象序列化为json。
2.Javascript读取json对象var product = eval(callbackmsg)。
3.Javascript读取json值callbackmsg[u].Name。