自定义模型转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); }