JsonHelper.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using Newtonsoft.Json;
  2. namespace ProductionLineMonitor.Core.Utils
  3. {
  4. public static class JsonHelper
  5. {
  6. /// <summary>
  7. /// 将JSON字符串反序列化成对象
  8. /// </summary>
  9. /// <typeparam name="T"></typeparam>
  10. /// <param name="baseEntity"></param>
  11. /// <param name="strJson"></param>
  12. /// <returns></returns>
  13. public static T ToObject<T>(T baseEntity, string strJson)
  14. {
  15. return JsonConvert.DeserializeAnonymousType(strJson, baseEntity);
  16. }
  17. /// <summary>
  18. /// 将Json反序列化成对象
  19. /// </summary>
  20. /// <typeparam name="T"></typeparam>
  21. /// <param name="strJson"></param>
  22. /// <returns></returns>
  23. public static T ToObject<T>(this string strJson)
  24. {
  25. return JsonConvert.DeserializeObject<T>(strJson);
  26. }
  27. /// <summary>
  28. /// 将对象转换层JSON字符串
  29. /// </summary>
  30. /// <typeparam name="T"></typeparam>
  31. /// <param name="data"></param>
  32. /// <returns></returns>
  33. public static string ToJson<T>(this T data)
  34. {
  35. return JsonConvert.SerializeObject(data);
  36. }
  37. }
  38. }