星火微课系统客户端
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

JsonHelper.cs 2.0KB

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