/// <summary> /// 将文件反序列化json /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="path">json所存放的路径</param> /// <returns>实体</returns> private static T DeSerializableJson <T>(string path) { T t = default(T); try { FileInfo fi = new FileInfo(path); if (fi.Exists) { StreamReader sr = new StreamReader(path, Encoding.UTF8); String line; StringBuilder strcontent = new StringBuilder(); while ((line = sr.ReadLine()) != null) { strcontent.Append(line.ToString()); } t = JsonAndXmlSerialization.JsonDeserialize <T>(strcontent.ToString()); } } catch (Exception ex) { throw ex; } return(t); }
/// <summary> /// 将字符串根据格式(XML/JSON)反序列化成指定类型的对象 /// </summary> /// <typeparam name="T">指定类型</typeparam> /// <param name="s">目标字符串</param> /// <param name="format">输入格式</param> /// <returns></returns> public static T Deserialize <T>(string s, string format = "JSON") { if (format.ToLower() == "xml") { return(JsonAndXmlSerialization.XmlDeserialize <T>(s)); } else { return(JsonAndXmlSerialization.JsonDeserialize <T>(s)); } }