Exemplo n.º 1
0
 /// <summary>
 /// 将字符串反序列化为指定的对象
 /// </summary>
 /// <param name="value"></param>
 /// <typeparam name="T">指定的对象</typeparam>
 /// <returns></returns>
 public static T DeserializeObject <T>(string value)
 {
     if (!string.IsNullOrEmpty(value))
     {
         try
         {
             return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(value));
         }
         catch (Exception ex)
         {
             logger.LogError(ex, "反序列化JSON失败");
             throw FrameworkException.CreateNew($"反序列化JSON失败,值:{value}");
         }
     }
     return(default(T));
 }
Exemplo n.º 2
0
        /// <summary>
        /// 将字符串反序列化为指定的对象
        /// </summary>
        /// <param name="value"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static object DeserializeObject(string value, Type t)
        {
            //保持与上面函数功能一致
            if (string.IsNullOrEmpty(value))
            {
                return(t.IsValueType ? Activator.CreateInstance(t) : null);
            }

            try
            {
                return(Newtonsoft.Json.JsonConvert.DeserializeObject(value, t));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "反序列化JSON失败");
                throw FrameworkException.CreateNew($"反序列化JSON失败,值:{value}");
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// </summary>
 /// <typeparam name="T">类型</typeparam>
 /// <param name="xml">xml文档</param>
 /// <returns></returns>
 public static T XmlToDeserializeObject <T>(string xml)
 {
     try
     {
         System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
         doc.LoadXml(xml);
         if (doc.DocumentElement != null)
         {
             string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
             return(JsonConvert.DeserializeObject <T>(json));
         }
     }
     catch (Exception ex)
     {
         logger.LogError(ex, "反序列化XML失败");
         throw FrameworkException.CreateNew($"反序列化XML失败,值:{xml}", ex);
     }
     return(default(T));
 }