using Newtonsoft.Json; using System; using System.IO; namespace Production_Oil.Common { public class JsonHelper { /// /// 读取Json文件 /// /// 文件路径 /// public static string ReadJsonFile(string filePath) { try { if (!File.Exists(filePath)) { throw new FileNotFoundException(filePath); } return File.ReadAllText(filePath); } catch (Exception) { return string.Empty; } } /// /// 将实体类序列化字符串 /// /// 实体类 /// /// public static string SerializeObject(T t) { try { return JsonConvert.SerializeObject(t); } catch (Exception ex) { return string.Empty; } } /// /// 将对象序列化为json格式 /// /// 序列化对象 /// json字符串 public static string SerializeObjct(object obj) { try { return JsonConvert.SerializeObject(obj); } catch (Exception) { return string.Empty; } } /// /// 解析JSON字符串生成对象实体 /// /// 实体类 /// JSON字符串 /// public static T JsonConvertObject(string json) { try { return JsonConvert.DeserializeObject(json); } catch (Exception) { throw; } } } }