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

54 lines
1.1 KiB

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