JsonExtensions.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Converters;
  3. using Newtonsoft.Json.Linq;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Reflection;
  8. using WCS.Entity.Protocol;
  9. namespace WCS_Client
  10. {
  11. /// <summary>
  12. /// 扩展.json序列反序列化
  13. /// </summary>
  14. public static partial class JsonExtensions
  15. {
  16. /// <summary>
  17. /// 转成json对象
  18. /// </summary>
  19. /// <param name="Json">json字串</param>
  20. /// <returns></returns>
  21. public static object ToJson(this string Json)
  22. {
  23. return Json == null ? null : JsonConvert.DeserializeObject(Json);
  24. }
  25. /// <summary>
  26. /// 转成json字串
  27. /// </summary>
  28. /// <param name="obj">对象</param>
  29. /// <returns></returns>
  30. public static string ToJson(this object obj)
  31. {
  32. var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
  33. return JsonConvert.SerializeObject(obj, timeConverter);
  34. }
  35. /// <summary>
  36. /// 转成json字串
  37. /// </summary>
  38. /// <param name="obj">对象</param>
  39. /// <param name="datetimeformats">时间格式化</param>
  40. /// <returns></returns>
  41. public static string ToJson(this object obj, string datetimeformats)
  42. {
  43. var timeConverter = new IsoDateTimeConverter { DateTimeFormat = datetimeformats };
  44. return JsonConvert.SerializeObject(obj, timeConverter);
  45. }
  46. /// <summary>
  47. /// 字串反序列化成指定对象实体
  48. /// </summary>
  49. /// <typeparam name="T">实体类型</typeparam>
  50. /// <param name="Json">字串</param>
  51. /// <returns></returns>
  52. public static T ToObject<T>(this string Json)
  53. {
  54. return Json == null ? default(T) : JsonConvert.DeserializeObject<T>(Json);
  55. }
  56. /// <summary>
  57. /// 字串反序列化成指定对象实体(列表)
  58. /// </summary>
  59. /// <typeparam name="T">实体类型</typeparam>
  60. /// <param name="Json">字串</param>
  61. /// <returns></returns>
  62. public static List<T> ToList<T>(this string Json)
  63. {
  64. return Json == null ? null : JsonConvert.DeserializeObject<List<T>>(Json);
  65. }
  66. /// <summary>
  67. /// 字串反序列化成DataTable
  68. /// </summary>
  69. /// <param name="Json">字串</param>
  70. /// <returns></returns>
  71. public static DataTable ToTable(this string Json)
  72. {
  73. return Json == null ? null : JsonConvert.DeserializeObject<DataTable>(Json);
  74. }
  75. /// <summary>
  76. /// 字串反序列化成linq对象
  77. /// </summary>
  78. /// <param name="Json">字串</param>
  79. /// <returns></returns>
  80. public static JObject ToJObject(this string Json)
  81. {
  82. return Json == null ? JObject.Parse("{}") : JObject.Parse(Json.Replace("&nbsp;", ""));
  83. }
  84. public static DataTable GetAttributesDataTable(this SCData obj)
  85. {
  86. var tb = new DataTable(obj.GetType().Name);
  87. tb.Columns.Add("信号说明", typeof(string));
  88. tb.Columns.Add("信号名称", typeof(string));
  89. tb.Columns.Add("信号值", typeof(string));
  90. foreach (PropertyInfo pi in obj.D521.GetType().GetProperties())
  91. {
  92. AttributeCollection attributes = TypeDescriptor.GetProperties(obj.D521.GetType())[pi.Name].Attributes;
  93. DescriptionAttribute myAttribute = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
  94. string describle = myAttribute.Description;
  95. string proname = pi.Name;
  96. var abc = pi.GetValue(obj.D521, null);
  97. switch (proname)
  98. {
  99. default:
  100. break;
  101. }
  102. var values = new object[3];
  103. values[0] = describle;
  104. values[1] = proname;
  105. values[2] = abc;
  106. tb.Rows.Add(values);
  107. }
  108. foreach (PropertyInfo pi in obj.D520.GetType().GetProperties())
  109. {
  110. AttributeCollection attributes = TypeDescriptor.GetProperties(obj.D520.GetType())[pi.Name].Attributes;
  111. DescriptionAttribute myAttribute = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
  112. string describle = myAttribute.Description;
  113. string proname = pi.Name;
  114. var abc = pi.GetValue(obj.D520, null);
  115. switch (proname)
  116. {
  117. default:
  118. break;
  119. }
  120. var values = new object[3];
  121. values[0] = describle;
  122. values[1] = proname;
  123. values[2] = abc;
  124. tb.Rows.Add(values);
  125. }
  126. foreach (PropertyInfo pi in obj.D537.GetType().GetProperties())
  127. {
  128. AttributeCollection attributes = TypeDescriptor.GetProperties(obj.D537.GetType())[pi.Name].Attributes;
  129. DescriptionAttribute myAttribute = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
  130. string describle = myAttribute.Description;
  131. string proname = "DB537" + pi.Name;
  132. var abc = pi.GetValue(obj.D537, null);
  133. switch (pi.Name)
  134. {
  135. case "TASKNUM":
  136. describle = "任务号";
  137. break;
  138. }
  139. var values = new object[3];
  140. values[0] = describle;
  141. values[1] = proname;
  142. values[2] = abc;
  143. tb.Rows.Add(values);
  144. }
  145. //属性遍历
  146. return tb;
  147. }
  148. }
  149. }