78 lines
1.9 KiB
C#
78 lines
1.9 KiB
C#
![]() |
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Newtonsoft.Json;
|
|||
|
namespace Epost.Common
|
|||
|
{
|
|||
|
#region Json
|
|||
|
/// <summary>
|
|||
|
/// Json
|
|||
|
/// </summary>
|
|||
|
public class JsonHelper
|
|||
|
{
|
|||
|
#region 序列化对象
|
|||
|
/// <summary>
|
|||
|
/// 序列化对象
|
|||
|
/// </summary>
|
|||
|
/// <param name="obj"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string SerializeObject(object obj)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
return JsonConvert.SerializeObject(obj);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
|
|||
|
return string.Empty;
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 反序列化对象
|
|||
|
/// <summary>
|
|||
|
/// 反序列化对象
|
|||
|
/// </summary>
|
|||
|
/// <param name="str"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static T DeserializeObject<T>(string str) where T : new()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
return JsonConvert.DeserializeObject<T>(str);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
|
|||
|
return new T();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 反序列化对象
|
|||
|
/// </summary>
|
|||
|
/// <param name="str"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static object DeserializeObject(string str)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
return JsonConvert.DeserializeObject(str);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
|
|||
|
return new object();
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
public static T GetObjectFromJson<T>(string json)
|
|||
|
{
|
|||
|
return JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
}
|