使用DataContractJsonSerializer进行JSON序列化的JSONHelper类

前端之家收集整理的这篇文章主要介绍了使用DataContractJsonSerializer进行JSON序列化的JSONHelper类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public static class JSONHelper
{
/// <summary>
/// 将对象转化为Json字符串
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="instanse">对象本身</param>
/// <returns>JSON字符串</returns>
public static string Serializer<T>(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;
}
}
/// <summary>
/// 将字符串转化为JSON对象,如果转换失败,返回default(T)
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="s">字符串</param>
/// <returns>转换值</returns>
public static T Deserializer<T>(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);
}
}
}

猜你在找的Json相关文章