• 序列化上面创建的Person对象,使其成为一个JSON字符串


    <%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Sample.aspx.cs"
        Inherits="ClientScripting_SysSerialization_Sample" Title="Sys.Serialization命名空间下的类" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

        <script runat="Server">
            protected void Button1_Click(object sender, EventArgs e)
            {
                // 创建一个Person对象
                Person person = new Person();
                person.Name = "webabcd";
                person.Age = 27;

                // 序列化上面创建的Person对象,使其成为一个JSON字符串
                System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
                string s = jss.Serialize(person);
                // 注册DataItem
                ScriptManager.GetCurrent(this).RegisterDataItem(this.UpdatePanel1, s);
            }

            /// <summary>
            /// Person类
            /// </summary>
            public class Person
            {
                private string _name;
                /// <summary>
                /// 姓名
                /// </summary>
                public string Name
                {
                    get { return _name; }
                    set { _name = value; }
                }

                private int _age;
                /// <summary>
                /// 年龄
                /// </summary>
                public int Age
                {
                    get { return _age; }
                    set { _age = value; }
                }

            }
        </script>

        <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="Server">
            <ContentTemplate>
                <%= DateTime.Now.ToString()%>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Sys.Serialization" OnClick="Button1_Click" />
            </ContentTemplate>
        </asp:UpdatePanel>
        <p>
            <textarea id="TraceConsole" style=" 500px; height: 100px;"></textarea>
        </p>

        <script language="javascript" type="text/javascript">

            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
            function endRequestHandler(sender, e)
            {
                var upId = "<%= this.UpdatePanel1.ClientID %>";
                // 获得服务端注册的DataItem,它是一个JSON字符串
                var jsonStringServer = e.get_dataItems()[upId];
                // 反序列化这个JSON字符串,使其成为一个JSON对象
                var jsonObject = Sys.Serialization.JavaScriptSerializer.deserialize(jsonStringServer)

                // 序列化这个JOSN对象,使其成为一个JSON字符串
                var jsonStringClient = Sys.Serialization.JavaScriptSerializer.serialize(jsonObject);
                Sys.Debug.trace(jsonStringClient);
                Sys.Debug.trace(jsonObject.Name + " " + jsonObject.Age);
                Sys.Debug.trace(Sys.Serialization.JavaScriptSerializer.serialize(jsonObject));
            }

        </script>

    </asp:Content>

  • 相关阅读:
    专门针对初学者的Node.js教程
    windows版的node.js简单示例
    bzoj 1003: [ZJOI2006]物流运输【spfa+dp】
    bzoj 3573: [Hnoi2014]米特运输【树形dp+瞎搞】
    bzoj 1082: [SCOI2005]栅栏【二分+dfs】
    bzoj 2440: [中山市选2011]完全平方数【莫比乌斯函数+二分】
    bzoj 1049: [HAOI2006]数字序列【dp+二分+瞎搞】
    bzoj 2588: Spoj 10628. Count on a tree【主席树+倍增】
    bzoj 4551: [Tjoi2016&Heoi2016]树【并查集】
    bzoj 4310: 跳蚤【后缀数组+st表+二分+贪心】
  • 原文地址:https://www.cnblogs.com/zhukezhuke/p/1544491.html
Copyright © 2020-2023  润新知