zoukankan      html  css  js  c++  java
  • 序列化上面创建的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>

  • 相关阅读:
    hdu1828(线段树——矩形周长并)
    hdu1255(线段树——矩形面积交)
    用jQuery获取到一个类名获取到的是一个数组 ,如果对数组中的每个进行相应的操作可以这样进行
    CSS3向外扩散的圆
    鼠标放上去图片会放大
    Django分页
    Django使用富文本编辑器
    Django日志配置
    Linux中的文件类型
    Linux压缩和解压缩
  • 原文地址:https://www.cnblogs.com/zhukezhuke/p/1544491.html
Copyright © 2011-2022 走看看