/// <summary>
/// 将Json字符串转换为实体对象
/// </summary>
/// <typeparam name="T">实体对象类</typeparam>
/// <param name="jsonStr">json字符串</param>
/// <param name="isThrowException">出现异常时是否抛到上级</param>
/// <returns></returns>
public static T JsonToObject<T>(string jsonStr, bool isThrowException = false)
{
T result = default(T);
try
{
result = JsonConvert.DeserializeObject<T>(jsonStr);
}
catch (Exception ex)
{
if (isThrowException)
{
throw ex;
}
}
return result;
}
/// <summary>
/// 将对象序列化为json字符串
/// </summary>
/// <param name="obj"></param>
/// <param name="isThrowException">出现异常时是否抛到上级</param>
/// <returns></returns>
public static string ObjectToJson(object obj, bool isThrowException = false)
{
string result = string.Empty;
try
{
result = JsonConvert.SerializeObject(obj);
}
catch (Exception ex)
{
if (isThrowException)
{
throw ex;
}
}
return result;
}