平安校园
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.

ObjectExtension.cs 1.1 KiB

4 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 
  2. //
  3. namespace SafeCampus.Core;
  4. /// <summary>
  5. /// object拓展
  6. /// </summary>
  7. public static class ObjectExtension
  8. {
  9. /// <summary>
  10. /// json字符串序列化
  11. /// </summary>
  12. /// <param name="json"></param>
  13. /// <returns></returns>
  14. public static object ToObject(this string json)
  15. {
  16. return string.IsNullOrEmpty(json) ? null : JsonConvert.DeserializeObject(json);
  17. }
  18. /// <summary>
  19. /// json字符串序列化
  20. /// </summary>
  21. /// <typeparam name="T"></typeparam>
  22. /// <param name="json"></param>
  23. /// <returns></returns>
  24. public static T ToObject<T>(this string json)
  25. {
  26. if (json != null)
  27. {
  28. json = json.Replace("&nbsp;", "");
  29. return JsonConvert.DeserializeObject<T>(json);
  30. }
  31. return default;
  32. }
  33. /// <summary>
  34. /// json字符串序列化
  35. /// </summary>
  36. /// <param name="json"></param>
  37. /// <returns></returns>
  38. public static JObject ToJObject(this string json)
  39. {
  40. return json == null ? JObject.Parse("{}") : JObject.Parse(json.Replace("&nbsp;", ""));
  41. }
  42. }