纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.5 KiB

1 year ago
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Converters;
  3. using Newtonsoft.Json.Linq;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. namespace NFine.Code
  7. {
  8. public static class Json
  9. {
  10. public static object ToJson(this string Json)
  11. {
  12. return Json == null ? null : JsonConvert.DeserializeObject(Json);
  13. }
  14. public static string ToJson(this object obj)
  15. {
  16. var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
  17. return JsonConvert.SerializeObject(obj, timeConverter);
  18. }
  19. public static string ToJson(this object obj, string datetimeformats)
  20. {
  21. var timeConverter = new IsoDateTimeConverter { DateTimeFormat = datetimeformats };
  22. return JsonConvert.SerializeObject(obj, timeConverter);
  23. }
  24. public static T ToObject<T>(this string Json)
  25. {
  26. return Json == null ? default(T) : JsonConvert.DeserializeObject<T>(Json);
  27. }
  28. public static List<T> ToList<T>(this string Json)
  29. {
  30. return Json == null ? null : JsonConvert.DeserializeObject<List<T>>(Json);
  31. }
  32. public static DataTable ToTable(this string Json)
  33. {
  34. return Json == null ? null : JsonConvert.DeserializeObject<DataTable>(Json);
  35. }
  36. public static JObject ToJObject(this string Json)
  37. {
  38. return Json == null ? JObject.Parse("{}") : JObject.Parse(Json.Replace("&nbsp;", ""));
  39. }
  40. }
  41. }