使用DataContractJsonSerializer进行JSON序列化的JSONHelper类
public static class JSONHelper { ////// 将对象转化为Json字符串 /// ///对象类型 /// 对象本身 ///JSON字符串 public static string Serializer(this T instanse) { try { DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream()) { js.WriteObject(ms, instanse); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); StreamReader sr = new StreamReader(ms); return sr.ReadToEnd(); } } catch { return String.Empty; } } /// /// 将字符串转化为JSON对象,如果转换失败,返回default(T) /// ///对象类型 /// 字符串 ///转换值 public static T Deserializer(this string s) { try { DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream()) { StreamWriter sw = new StreamWriter(ms); sw.Write(s); sw.Flush(); ms.Seek(0, SeekOrigin.Begin); return (T)js.ReadObject(ms); } } catch { return default(T); } } }
三沙ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
当前名称:使用DataContractJsonSerializer进行JSON序列化的JSONHelper类
文章出自:http://pwwzsj.com/article/jhsheg.html