zoukankan      html  css  js  c++  java
  • C#通用Json格式序列化和反序列化的方法

    引入 System.Runtime.Serialization.Json命名空间使用 DataContractJsonSerializer类实现序列化

    序列化类:People

    public class People
    {
    public int Id { get; set; }
    public string Name { get; set; }
    }
    执行序列化反序列化
    protected void Page_Load(object sender, EventArgs e)
    {
    People people
    = new People();
    people.Id
    = 1;
    people.Name
    = "小白";


    DataContractJsonSerializer json
    = new DataContractJsonSerializer(people.GetType());
    string szJson = "";
    //序列化
    using (MemoryStream stream = new MemoryStream())
    {
    json.WriteObject(stream, people);
    szJson
    = Encoding.UTF8.GetString(stream.ToArray());
    }
    //反序列化
    using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
    {
    DataContractJsonSerializer serializer
    = new DataContractJsonSerializer(typeof(People));
    People _people
    = (People)serializer.ReadObject(ms);
    }
    }

    szJson输出结果:{"Id":1,"Name":"小白"}


    可以使用IgnoreDataMember:指定该成员不是数据协定的一部分且没有进行序列化,DataMember:定义序列化属性参数,使用DataMember属性标记字段必须使用DataContract标记类
    否则DataMember标记不起作用。

    [DataContract]
    public class People
    {
    [DataMember(Name
    = "id")]
    public int Id { get; set; }
    [IgnoreDataMember]
    public string Name { get; set; }
    }

    输出结果: {"id":1}

     

    进行简单封装一下

    using System.Text;
    using System.Runtime.Serialization.Json;
    using System.IO;

    namespace RRQ.Common
    {
        public static class JsonExtension
        {
            /// <summary>
            
    /// 序列化Json
            
    /// </summary>
            
    /// <typeparam name="T"></typeparam>
            
    /// <param name="target"></param>
            
    /// <returns></returns>
            public static string JsonSerialize<T>(this object target)
            {
                T result = (T)target;

                DataContractJsonSerializer json = new DataContractJsonSerializer(result.GetType());

                using (MemoryStream stream = new MemoryStream())
                {
                    json.WriteObject(stream, result);
                    return Encoding.UTF8.GetString(stream.ToArray());
                }
            }

            /// <summary>
            
    /// 返序列化Json
            
    /// </summary>
            
    /// <typeparam name="T"></typeparam>
            
    /// <param name="target"></param>
            
    /// <returns></returns>
            public static T JsonDeserialize<T>(this string target)
            {
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(target)))
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                    return (T)serializer.ReadObject(ms);
                }
            }
        }
    }
  • 相关阅读:
    [转]经典SQL语句大全
    【转】windows 7系统安装与配置Tomcat服务器环境
    [转]php连接postgresql
    win7(64位)php5.5-Apache2.4-环境安装
    [转]WIN7系统安装Apache 提示msvcr110.DLL
    【转】如何在CentOS/RHEL中安装基于Web的监控系统 linux-das
    CentOS6.5安全策略设置
    【转】Lua编程规范
    在python中的使用
    游标 cursor
  • 原文地址:https://www.cnblogs.com/zhuiyi/p/3015045.html
Copyright © 2011-2022 走看看