星火直播PC
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.

JsonHelper.cs 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Newtonsoft.Json;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace Common.system
  5. {
  6. public class JsonHelper
  7. {
  8. #region Method
  9. /// <summary>
  10. /// 类对像转换成json格式
  11. /// </summary>
  12. /// <returns></returns>
  13. public static string ToJson(object t)
  14. {
  15. return JsonConvert.SerializeObject(
  16. t, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include }
  17. );
  18. }
  19. /// <summary>
  20. /// 类对像转换成json格式
  21. /// </summary>
  22. /// <param name="t"></param>
  23. /// <param name="HasNullIgnore">是否忽略NULL值</param>
  24. /// <returns></returns>
  25. public static string ToJson(object t, bool HasNullIgnore)
  26. {
  27. if (HasNullIgnore)
  28. {
  29. return JsonConvert.SerializeObject(t, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
  30. }
  31. else
  32. {
  33. return ToJson(t);
  34. }
  35. }
  36. /// <summary>
  37. /// json格式转换
  38. /// </summary>
  39. /// <typeparam name="T"></typeparam>
  40. /// <param name="strJson"></param>
  41. /// <returns></returns>
  42. public static T JsonToObj<T>(string strJson) where T : class
  43. {
  44. if (strJson != null && strJson != "") { return JsonConvert.DeserializeObject<T>(strJson); }
  45. return null;
  46. }
  47. internal static List<T> JsonToList<T>(string respstr)
  48. {
  49. JsonSerializer serializer = new JsonSerializer();
  50. StringReader sr = new StringReader(respstr);
  51. object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
  52. List<T> list = o as List<T>;
  53. return list;
  54. }
  55. #endregion Method
  56. }
  57. }