.Net Core 中的JSon与对象的互相转换

2019-05-23  本文已影响0人  懒懒的程序员一枚

Nuget 引入Newtonsoft.Json

创建 JsonHelper类

public class JsonHelper
    {
        /// <summary>
        /// 将实体类序列化为JSON
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        static public string SerializeJSON<T>(T data)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(data);
        }
 
        /// <summary>
        /// 反序列化JSON
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        static public T DeserializeJSON<T>(string json)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
        }
 
        /// <summary>
         /// 日期转换为时间戳(时间戳单位秒)
         /// </summary>
         /// <param name="TimeStamp"></param>
         /// <returns></returns>
         public static long ConvertToTimeStamp(DateTime time)
         {
             DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
             return (long) (time.AddHours(-8) - Jan1st1970).TotalMilliseconds;
         }
}
上一篇下一篇

猜你喜欢

热点阅读