1#
  1. public static class JsonHelper
  2.     {
  3.         public static string ToJson(object t)
  4.         {
  5.             try
  6.             {
  7.                 return new JavaScriptSerializer().Serialize(t);
  8.             }
  9.             catch (Exception ex)
  10.             {

  11.             }

  12.             return "";
  13.         }

  14.         public static TData FromJson<TData>(string json)
  15.         {
  16.             try
  17.             {
  18.                 return new JavaScriptSerializer().Deserialize<TData>(json);
  19.             }
  20.             catch (Exception ex)
  21.             {
  22.                 
  23.             }

  24.             return Activator.CreateInstance<TData>();
  25.         }
  26.     }
复制代码

使用方法:

  1. var userCode = JsonHelper.ToJson(user);
复制代码