• 【原】JavaScriptSerializer类的序列化和反序列化操作


    JavaScriptSerializer类为启用AJAX的应用程序提供序列化和反序列化的功能(MSDN原版:Provides serialization and deserialization functionality for AJAX-enabled applications.)深深的鄙视一下MSDN中文版的翻译(赫然写着"为启用 AFAX 的应用程序提供序列化和反序列化功能。",抓破头也没查出AFAX是个啥)

    使用时需要引入命名空间:

    using System.Web.Script.Serialization;

    最常用的几个方法:

    1)Deserialize(String, Type)    将 JSON 格式字符串转换为指定类型的对象。
    2)Deserialize<T>(String)    将指定的 JSON 字符串转换为 T 类型的对象。
    3)Serialize(Object)    将对象转换为 JSON 字符串。

    下面结合一个例子来说明这个类的用法(代码中有注释,就不再说明了):

    1)首先编写一个实体类

    using System;
    using System.Web.Script.Serialization;
    
    namespace JavaScriptSerializerDemo
    {
        /// <summary>
        /// 不论类是否使用[Serializable]特性,只要属性使用[ScriptIgnore]特性,则该属性不能被序列化
        /// </summary>
        [Serializable]
        public class User
        {
            public int UserID { get; set; }
            
            //[ScriptIgnore]
            public string UserName { get; set; }
        }
    }

    2)接着编写模拟数据

    /// <summary>
    /// 模拟单个实体
    /// </summary>
    /// <returns></returns>
    private User MockSingleData()
    {
        var user = new User { UserID = 1, UserName = "洪自军" };
        return user;
    }
    
    /// <summary>
    /// 模拟实体集合
    /// </summary>
    /// <returns></returns>
    private List<User> MockListData()
    {
        var list = new List<User>
        {
            new User { UserID = 1, UserName = "张洋" },
            new User { UserID = 2, UserName = "张兄家" },
            new User { UserID = 3, UserName = "王生杰" },
            new User { UserID = 4, UserName = "叶宇" }
        };
    
        return list;
    }
    
    /// <summary>
    /// 模拟测试实体集合(集合中有50000个实体)
    /// </summary>
    /// <returns></returns>
    private object MockTestData()
    {
        var list = new List<User>();
    
        for (int i = 0; i < 50000; i++)
        {
            list.Add(new User { UserID = i, UserName = "测试姓名_" + i });
        }
    
        return list;
    }

    3)然后编写序列化和反序列化数据的处理代码

    /// <summary>
    /// 页面初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            JavaScriptSerializer ser = new JavaScriptSerializer();
    
            Response.Write("1)序列化取得单个实体的JSON字符串:<br/>");
            Response.Write(ser.Serialize(MockSingleData()));
    
            Response.Write("<hr/>");
    
            User user = ser.Deserialize<User>(ser.Serialize(MockSingleData()));
            Response.Write("2)反序列化取得JSON字符串对应的单个实体:<br/>");
            Response.Write(user.UserID + "<br/>" + user.UserName);
    
            Response.Write("<hr/>");
    
            Response.Write("3)序列化取得实体集合的JSON字符串:<br/>");
            Response.Write(ser.Serialize(MockListData()));
    
            Response.Write("<hr/>");
    
            TimeSpan tsStart = new TimeSpan(DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond);
    
            Response.Write("4)序列化取得测试实体集合(数量50000个实体)的JSON字符串:<br/>");
            ser.Serialize(MockTestData());
    
            TimeSpan tsEnd = new TimeSpan(DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond);
            
            TimeSpan ts = tsStart.Subtract(tsEnd).Duration();
            Response.Write(string.Format("使用JavaScriptSerializer变换耗时:{0}", ts.ToString()));
    
        }
    }

    4)运行效果如图所示

  • 相关阅读:
    Linux
    网络
    线程池
    JVM内存结构相关知识
    JVM命令
    maven
    多线程
    AJAX、JSON
    JSP、EL、JSTL
    Mysql面试总结
  • 原文地址:https://www.cnblogs.com/iflytek/p/3089919.html
Copyright © 2020-2023  润新知