对象转JSON字符串
/// <summary> /// 对象转Json字符串 /// </summary> /// <param name="obj"></param> /// <param name="isCamel"></param> /// <returns></returns> public static string ToJsonString(object obj, bool isCamel = false) { var settings = new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss", ReferenceLoopHandling = ReferenceLoopHandling.Ignore }; if (isCamel) { settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); } return JsonConvert.SerializeObject(obj, settings); } /// <summary> /// Jons字符串转对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonString"></param> /// <returns></returns> public static T ToObject<T>(string jsonString) { return JsonConvert.DeserializeObject<T>(jsonString); } public static string ToJson(this object obj, bool isCamel = false) { var settings = new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss", ReferenceLoopHandling = ReferenceLoopHandling.Ignore }; if (isCamel) { settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); } return JsonConvert.SerializeObject(obj, settings); }