You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.6 KiB

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
namespace EC.Helper.Common
{
public class JsonHelper
{
/// <summary>
/// Object To Json
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string ToJson(object obj)
{
return JsonConvert.SerializeObject(obj);
}
/// <summary>
/// Json To Object
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static object ToObject(string json)
{
return !string.IsNullOrEmpty(json) ? JsonConvert.DeserializeObject(json) : default;
}
/// <summary>
/// Json To T Object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static T ToObject<T>(string json) where T : class
{
return !string.IsNullOrEmpty(json) ? JsonConvert.DeserializeObject<T>(json) : default;
}
/// <summary>
/// Json To JObject
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static JObject ToJObject(string json)
{
return ToObject<JObject>(json);
}
/// <summary>
/// Json To Object List
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static List<T> ToList<T>(string json) where T : class
{
return ToObject<List<T>>(json);
}
/// <summary>
/// 深克隆
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static T DeepClone<T>(T obj) where T : class
{
string json = ToJson(obj);
return ToObject<T>(json);
}
}
}