Json序列反序列类型处理帮助类

时间:2021-08-28 14:50:01

Json序列反序列类型处理帮助类。

  • JSON反序列化
  • JSON序列化
  • 将Json序列化的时间由/Date(1294499956278+0800)转为字符串
  • 将时间字符串转为Json时间
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.IO;
    using System.Runtime.Serialization.Json;
    using System.Web.Script.Serialization; namespace IM.Common
    {
    public class JsonArryHelper
    {
    /// <summary>
    /// JSON序列化
    /// </summary>
    public static string JsonSerializer<T>(T t)
    {
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
    MemoryStream ms = new MemoryStream();
    ser.WriteObject(ms, t);
    string jsonString = Encoding.UTF8.GetString(ms.ToArray());
    ms.Close();
    //替换Json的Date字符串
    string p = @"\\/Date\((\d+)\+\d+\)\\/";
    MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
    Regex reg = new Regex(p);
    jsonString = reg.Replace(jsonString, matchEvaluator);
    return jsonString;
    } /// <summary>
    /// JSON反序列化
    /// </summary>
    /// <param name="json"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static T Deserialize<T>(string json)
    {
    var obj = Activator.CreateInstance<T>();
    using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
    {
    var serializer = new DataContractJsonSerializer(obj.GetType());
    return (T)serializer.ReadObject(ms);
    } } /// <summary>
    /// JSON反序列化
    /// </summary>
    public static T JsonDeserialize<T>(string jsonString)
    {
    //将"yyyy-MM-dd HH:mm:ss"格式的字符串转为"\/Date(1294499956278+0800)\/"格式
    string p = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}";
    MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertDateStringToJsonDate);
    Regex reg = new Regex(p);
    jsonString = reg.Replace(jsonString, matchEvaluator);
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
    T obj;
    try
    {
    obj = (T)ser.ReadObject(ms);
    }
    catch
    {
    obj = default(T);
    }
    return obj;
    }
    /// <summary>
    /// 将Json序列化的时间由/Date(1294499956278+0800)转为字符串
    /// </summary>
    private static string ConvertJsonDateToDateString(Match m)
    {
    string result = string.Empty;
    DateTime dt = new DateTime(, , );
    dt = dt.AddMilliseconds(long.Parse(m.Groups[].Value));
    dt = dt.ToLocalTime();
    result = dt.ToString("yyyy-MM-dd HH:mm:ss");
    return result;
    }
    /// <summary>
    /// 将时间字符串转为Json时间
    /// </summary>
    private static string ConvertDateStringToJsonDate(Match m)
    {
    string result = string.Empty;
    DateTime dt = DateTime.Parse(m.Groups[].Value);
    dt = dt.ToUniversalTime();
    TimeSpan ts = dt - DateTime.Parse("1970-01-01");
    result = string.Format("\\/Date({0}+0800)\\/", ts.TotalMilliseconds);
    return result;
    } public static List<T> JSONStringToList<T>(string jsonStr)
    {
    var serializer = new JavaScriptSerializer();
    var objs = serializer.Deserialize<List<T>>(jsonStr);
    return objs;
    }
    }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Newtonsoft.Json;
    using System.IO;
    using System.Web.Mvc;
    using System.Globalization; namespace IM.Common
    {
    /// <summary>
    /// 序列化操作类
    /// <para>创建作者:changjin</para>
    /// <para>创建日期:2010/10/25</para>
    /// </summary>
    public class JsonSerialize
    {
    /// <summary>
    /// 序列化对象
    /// <para>创建作者:changjin</para>
    /// <para>创建日期:2010/12/28</para>
    /// </summary>
    /// <param name="obj">序列化的对象</param>
    /// <returns>序列化后的json字符串</returns>
    public static String SerializeObject(Object obj)
    {
    //StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
    //JsonSerializer jsonSerializer = new JsonSerializer();
    //jsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    //JsonWriter jsonWriter = // jsonWriter.Formatting = Formatting.Indented;
    // jsonSerializer.Serialize(jsonWriter, obj); //return sw.ToString();
    return JsonConvert.SerializeObject(obj);
    }
    public static String SerializeObject1(Object obj)
    {
    //StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
    //JsonSerializer jsonSerializer = new JsonSerializer();
    //jsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    //JsonWriter jsonWriter = // jsonWriter.Formatting = Formatting.Indented;
    // jsonSerializer.Serialize(jsonWriter, obj); //return sw.ToString();
    return JsonConvert.SerializeObject(obj, Formatting.Indented);
    }
    /// <summary>
    /// 对象反序列化
    /// <para>创建作者:changjin</para>
    /// <para>创建日期:2010/12/28</para>
    /// </summary>
    /// <param name="jsonString">json对象字符串</param>
    /// <param name="type">反序列化对象类型</param>
    /// <returns>反序列化后的对象</returns>
    public static Object DeserializeObject(String jsonString, Type type)
    {
    return JsonConvert.DeserializeObject(jsonString, type.GetType());
    } /// <summary>
    /// 对象反序列化
    /// <para>创建作者:changjin</para>
    /// <para>创建日期:2010/12/28</para>
    /// </summary>
    /// <param name="jsonString">json对象字符串</param>
    /// <param name="type">反序列化对象类型</param>
    /// <returns>反序列化后的对象</returns>
    public static T DeserializeObject<T>(String jsonString)
    {
    return JsonConvert.DeserializeObject<T>(jsonString);
    } }
    }