Json序列化及反序列化(备忘)

本文详细探讨了Json序列化和反序列化,分别使用DataContractJsonSerializer、JavaScriptSerializer和Newtonsoft.Json进行操作。文章指出DataContractJsonSerializer的日期时间格式问题、属性顺序和数据成员的使用,JavaScriptSerializer的匿名对象序列化能力,以及Newtonsoft.Json在处理日期时间格式上的优势和反序列化匿名对象的方法。
摘要由CSDN通过智能技术生成

1.DataContractJsonSerializer

DataContractJsonSerializer在System.Runtime.Serialization.Json命名空间下,.NETFramework 3.5包含在System.ServiceModel.Web.dll中,需要添加对其的引用;.NETFramework 4在System.Runtime.Serialization中

序列化对象示例:

    public class Person
    {
        public int Age { get; set; }
        public string Name { get; set; }
        /// <summary>
        /// 涉及时间转换问题
        /// </summary>
        public DateTime BirthDay { get; set; }
        /// <summary>
        /// Dictionary
        /// </summary>
        public Dictionary<string, int> Subjects { get; set; }
        public Address Adress { get; set; }
        /// <summary>
        /// 集合类型
        /// </summary>
        public List<Role> RoleList { get; set; }
        /// <summary>
        /// 枚举接口
        /// </summary>
        public IEnumerable<Department> Departments { get; set; }
        /// <summary>
        /// 数组问题
        /// </summary>
        public string[] favoriteFruit { get; set; }

    }
    public class Role
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    public class Address
    {
        public string Province { get; set; }
        public string City { get; set; }
        public string District { get; set; }
    }
    public class Department
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
简单封装下DataContractSerializer,如下:

public static class JsonHelper
{
        public static T DeserializeFromString<T>(string str) where T : class
        {
            try
            {

                byte[] buffer = Encoding.UTF8.GetBytes(str);
                MemoryStream ms = new MemoryStream(buffer, 0, buffer.Length);
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                return ser.ReadObject(ms) as T;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        public static string SerializeToString<T>(T t) where T : class
        {
            try
            {
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
                string result = string.Empty;
                using (MemoryStream ms = new MemoryStream())
                {
                    ser.WriteObject(ms, t);
                    result = Encoding.UTF8.GetString(ms.ToArray());
                }

                return result;
            }
            catch
            {
                return null;
            }
        }
}

序列化代码如下:

            Person p = new Person();
            p.Adress = new Address
            {
                Province = "江西",
                City = "九江",
                District = "D1"
            };
            p.Age = 20;
            p.Name = "张三";
            p.BirthDay = DateTime.Parse("1990-09-09 09:00:00");
            p.Subjects = new Dictionary<string, int> { { "java", 99 }, { ".net", 100 } };
            p.RoleList = new List<Role>() { new Role() { Id = 1, Name = "Role1" }, new Role { Id = 2, Name = "Role2" } };
            p.favoriteFruit = new string[] { "apple",
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值