自定义模型转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); } } }
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.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); } }
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); }
本文来自博客园,作者:码农阿亮,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/15149483.html
技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
开源库地址,欢迎点亮:
GitHub:https://github.com/ITMingliang
Gitee: https://gitee.com/mingliang_it
GitLab: https://gitlab.com/ITMingliang
建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群】。