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


    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}sd{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(1970, 1, 1);
                  dt = dt.AddMilliseconds(long.Parse(m.Groups[1].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[0].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);
              }
      
      
            
          }
      }
  • 相关阅读:
    Confluence无法打开编辑器,一直在转圈
    Xamarin.Forms中的ListView的ItemTrapped事件与ItemSelected事件的区别
    C#读取物理网卡信息及其对应的IP地址
    【Xamarin报错】visual studio android 模拟器部署卡住
    【Xamarin报错】AndroidManifest.xml : warning XA0101: @(Content) build action is not supported
    【Xamarin报错】 COMPILETODALVIK : UNEXPECTED TOP-LEVEL error java.lang.OutOfMemoryError: Java heap space
    【Xamarin报错】libpng warning : iCCP: Not recognizing known sRGB profile that has been edited
    子窗口调用父窗口
    Windows Phone 8.1 多媒体(3):音乐
    Windows Phone 8.1 多媒体(1):相片
  • 原文地址:https://www.cnblogs.com/huhangfei/p/4997191.html
Copyright © 2020-2023  润新知