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);
}
}
}