.Net Framework 3.0的新特性,该命名空间提供了针对CLR托管类型的JSON(JavaScript Object Notation)的序列化和反序列化的方法。通过该命名空间提供的方法可以方便的实现服务器端托管类型数据与客户端浏览器之间JSON形式的数据传递。
以下是MSDN提供的实例代码片段:
序列化后的字符串在客户端显示效果如图:
参考文章:ICallback & JSON Based JavaScript Serialization:ASP alliance
以下是MSDN提供的实例代码片段:
1 <%@ Page Language="C#" %>
2 <%@ Import Namespace="System.Web.Script.Serialization" %>
3
4
5
6 <script runat="server">
7
8 JavaScriptSerializer serializer;
9 protected void Page_Load(object sender, EventArgs e)
10 {
11 serializer = new JavaScriptSerializer();
12
13 // Register the custom converter.
14 serializer.RegisterConverters(new JavaScriptConverter[] {
15 new System.Web.Script.Serialization.CS.ListItemCollectionConverter() });
16 this.SetFocus(TextBox1);
17 }
18
19 protected void saveButton_Click(object sender, EventArgs e)
20 {
21 // Save the current state of the ListBox control.
22 SavedState.Text = serializer.Serialize(ListBox1.Items);
23 recoverButton.Enabled = true;
24 Message.Text = "State saved";
25 }
26
27 protected void recoverButton_Click(object sender, EventArgs e)
28 {
29 //Recover the saved items of the ListBox control.
30 ListItemCollection recoveredList = serializer.Deserialize<ListItemCollection>(SavedState.Text);
31 ListItem[] newListItemArray = new ListItem[recoveredList.Count];
32 recoveredList.CopyTo(newListItemArray, 0);
33 ListBox1.Items.Clear();
34 ListBox1.Items.AddRange(newListItemArray);
35 Message.Text = "Last saved state recovered.";
36 }
37
38 protected void clearButton_Click(object sender, EventArgs e)
39 {
40 // Remove all items from the ListBox control.
41 ListBox1.Items.Clear();
42 Message.Text = "All items removed";
43 }
44
45 protected void ContactsGrid_SelectedIndexChanged(object sender, EventArgs e)
46 {
47 // Get the currently selected row using the SelectedRow property.
48 GridViewRow row = ContactsGrid.SelectedRow;
49
50 // Get the ID of item selected.
51 string itemId = ContactsGrid.DataKeys[row.RowIndex].Value.ToString();
52 ListItem newItem = new ListItem(row.Cells[4].Text, itemId);
53
54 // Check if the item already exists in the ListBox control.
55 if (!ListBox1.Items.Contains(newItem))
56 {
57 // Add the item to the ListBox control.
58 ListBox1.Items.Add(newItem);
59 Message.Text = "Item added";
60 }
61 else
62 Message.Text = "Item already exists";
63 }
64
65 protected void ContactsGrid_PageIndexChanged(object sender, EventArgs e)
66 {
67 //Reset the selected index.
68 ContactsGrid.SelectedIndex = -1;
69 }
70
71 protected void searchButton_Click(object sender, EventArgs e)
72 {
73 //Reset indexes.
74 ContactsGrid.SelectedIndex = -1;
75 ContactsGrid.PageIndex = 0;
76
77 //Set focus on the TextBox control.
78 ScriptManager1.SetFocus(TextBox1);
79 }
80
81 protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
82 {
83 // Show/hide the saved state string.
84 SavedState.Visible = CheckBox1.Checked;
85 StateLabel.Visible = CheckBox1.Checked;
86 }
87 </script>
2 <%@ Import Namespace="System.Web.Script.Serialization" %>
3
4
5
6 <script runat="server">
7
8 JavaScriptSerializer serializer;
9 protected void Page_Load(object sender, EventArgs e)
10 {
11 serializer = new JavaScriptSerializer();
12
13 // Register the custom converter.
14 serializer.RegisterConverters(new JavaScriptConverter[] {
15 new System.Web.Script.Serialization.CS.ListItemCollectionConverter() });
16 this.SetFocus(TextBox1);
17 }
18
19 protected void saveButton_Click(object sender, EventArgs e)
20 {
21 // Save the current state of the ListBox control.
22 SavedState.Text = serializer.Serialize(ListBox1.Items);
23 recoverButton.Enabled = true;
24 Message.Text = "State saved";
25 }
26
27 protected void recoverButton_Click(object sender, EventArgs e)
28 {
29 //Recover the saved items of the ListBox control.
30 ListItemCollection recoveredList = serializer.Deserialize<ListItemCollection>(SavedState.Text);
31 ListItem[] newListItemArray = new ListItem[recoveredList.Count];
32 recoveredList.CopyTo(newListItemArray, 0);
33 ListBox1.Items.Clear();
34 ListBox1.Items.AddRange(newListItemArray);
35 Message.Text = "Last saved state recovered.";
36 }
37
38 protected void clearButton_Click(object sender, EventArgs e)
39 {
40 // Remove all items from the ListBox control.
41 ListBox1.Items.Clear();
42 Message.Text = "All items removed";
43 }
44
45 protected void ContactsGrid_SelectedIndexChanged(object sender, EventArgs e)
46 {
47 // Get the currently selected row using the SelectedRow property.
48 GridViewRow row = ContactsGrid.SelectedRow;
49
50 // Get the ID of item selected.
51 string itemId = ContactsGrid.DataKeys[row.RowIndex].Value.ToString();
52 ListItem newItem = new ListItem(row.Cells[4].Text, itemId);
53
54 // Check if the item already exists in the ListBox control.
55 if (!ListBox1.Items.Contains(newItem))
56 {
57 // Add the item to the ListBox control.
58 ListBox1.Items.Add(newItem);
59 Message.Text = "Item added";
60 }
61 else
62 Message.Text = "Item already exists";
63 }
64
65 protected void ContactsGrid_PageIndexChanged(object sender, EventArgs e)
66 {
67 //Reset the selected index.
68 ContactsGrid.SelectedIndex = -1;
69 }
70
71 protected void searchButton_Click(object sender, EventArgs e)
72 {
73 //Reset indexes.
74 ContactsGrid.SelectedIndex = -1;
75 ContactsGrid.PageIndex = 0;
76
77 //Set focus on the TextBox control.
78 ScriptManager1.SetFocus(TextBox1);
79 }
80
81 protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
82 {
83 // Show/hide the saved state string.
84 SavedState.Visible = CheckBox1.Checked;
85 StateLabel.Visible = CheckBox1.Checked;
86 }
87 </script>
序列化后的字符串在客户端显示效果如图:
参考文章:ICallback & JSON Based JavaScript Serialization:ASP alliance