38 lines
1.1 KiB
C#
38 lines
1.1 KiB
C#
|
|
using Newtonsoft.Json;
|
|||
|
|
using Newtonsoft.Json.Linq;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Text;
|
|||
|
|
|
|||
|
|
namespace APT.Infrastructure.Core
|
|||
|
|
{
|
|||
|
|
public static class JsonExtensions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 把对象序列化成Json字符串格式
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="obj"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string ToJson(this object obj)
|
|||
|
|
{
|
|||
|
|
JsonSerializerSettings settings = new JsonSerializerSettings();
|
|||
|
|
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
|||
|
|
settings.NullValueHandling = NullValueHandling.Ignore;
|
|||
|
|
settings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
|||
|
|
return JsonConvert.SerializeObject(obj, settings);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 把Json字符串转换为强类型对象
|
|||
|
|
/// </summary>
|
|||
|
|
public static T DeserializeObject<T>(this string json)
|
|||
|
|
{
|
|||
|
|
return JsonConvert.DeserializeObject<T>(json);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static JObject ToJObject(this string Json)
|
|||
|
|
{
|
|||
|
|
return Json == null ? JObject.Parse("{}") : JObject.Parse(Json.Replace(" ", ""));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|