• 自定义模型转JSON 之Newtonsoft.Json.dll 、System.Runtime.Serialization.dll和System.Web.Extensions.dll


    自定义模型转JSON之Newtonsoft.Json.dll 、System.Runtime.Serialization.dll和System.Web.Extensions.dll

    自定义学生模型

      #region   自定义学生模型
            public enum Sex
            {
                男 = 0,
                女 = 1
            }
            [Serializable()]
            class Student
            {
                private string _name;
    
                public string Name
                {
                    get { return _name; }
                    set { _name = value; }
                }
    
                private int _age;
    
                public int Age
                {
                    get { return _age; }
                    set { _age = value; }
                }
    
                private Sex _sex = Sex.男;
    
                public Sex Sex
                {
                    get { return _sex; }
                    set { _sex = value; }
                }
                public Student()
                { }
                public Student(string name, int age, Sex sex)
                {
                    this._name = name;
                    this._age = age;
                    this._sex = sex;
                }
            }
    
            #endregion
    自定义学生模型

    Newtonsoft.Json.dll 

    public static class JsonNewtonsoft
        {
            /// <summary>
            /// 把对象转换为JSON字符串
            /// </summary>
            /// <param name="o">对象</param>
            /// <returns>JSON字符串</returns>
            public static string ToJSON(this object o)
            {
                if (o == null)
                {
                    return null;
                }
                return JsonConvert.SerializeObject(o);
            }
            /// <summary>
            /// 把Json文本转为实体
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="input"></param>
            /// <returns></returns>
            public static T FromJSON<T>(this string input)
            {
                try
                {
                    return JsonConvert.DeserializeObject<T>(input);
                }
                catch (Exception ex)
                {
                    return default(T);
                }
            }
        }
    Newtonsoft.Json.dll

    System.Runtime.Serialization.dll

     public static class JsonDataContractJsonSerializer
        {
            /// <summary>
            /// 内存对象转换为json字符串
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static string ObjectToJson(object obj)
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                MemoryStream stream = new MemoryStream();
                serializer.WriteObject(stream, obj);
                byte[] dataBytes = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(dataBytes, 0, (int)stream.Length);
                return Encoding.UTF8.GetString(dataBytes);
            }
            /// <summary>
            /// Json字符串转内存对象
            /// </summary>
            /// <param name="jsonString"></param>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static object JsonToObject(string jsonString, object obj)
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
                return serializer.ReadObject(mStream);
            }
        }
    System.Runtime.Serialization.dll

    System.Web.Extensions.dll

     public static class JsonJavaScriptSerializer
        {
            /// <summary>
            /// 内存对象转换为json字符串
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static string ToJSON(object obj)
            {
                StringBuilder sb = new StringBuilder();
                JavaScriptSerializer json = new JavaScriptSerializer();
                json.Serialize(obj, sb);
                return sb.ToString();
            }
            /// <summary>
            /// Json字符串转内存对象
            /// </summary>
            /// <param name="jsonString"></param>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static T FromJSON<T>(string jsonString)
            {
                JavaScriptSerializer json = new JavaScriptSerializer();
                return json.Deserialize<T>(jsonString);
            }
    
        }
    System.Web.Extensions.dll

    TEST

        static void test()
            {
                Student st = new Student("笨男孩", 22, Sex.男);
    
                //方法一
                string jsonStr = JsonNewtonsoft.ToJSON(st);//{"Name":"笨男孩","Age":22,"Sex":0}
                Student stobj = (Student)JsonNewtonsoft.FromJSON<Student>(jsonStr);
    
                //方法二
                string jsonStr2 = JsonDataContractJsonSerializer.ObjectToJson(st);   
                Student stobj2 = (Student)JsonDataContractJsonSerializer.JsonToObject(jsonStr2, st);//{"_age":22,"_name":"笨男孩","_sex":0}
            
                //方法三
                string jsonStr3 = JsonJavaScriptSerializer.ToJSON(st);//{"Name":"笨男孩","Age":22,"Sex":0}
                Student stobj3 = (Student)JsonJavaScriptSerializer.FromJSON<Student>(jsonStr3);
            }
    TEST

    技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
    我创建了一个.NET开发交流群,用于分享学习心得和讨论相关技术难题。欢迎有兴趣的小伙伴扫码入群,相互学习!

  • 相关阅读:
    使用JS实现图片轮播滚动跑马灯效果
    特殊字符和注释标签
    html常用标签总结
    html重点标签总结
    web常用开发工具
    标签分类和关系
    Web标准和骨架
    常用开浏览器内核
    常用浏览器介绍
    2019/08/20记
  • 原文地址:https://www.cnblogs.com/wml-it/p/15149483.html
Copyright © 2020-2023  润新知