Utility.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. namespace ddq
  9. {
  10. public class Utility
  11. {
  12. public static readonly Dictionary<int, string> RoleType = new Dictionary<int, string>
  13. {
  14. { 1, "Portfolio Manager" },
  15. { 2, "Researcher"},
  16. { 3, "Contactor"},
  17. { 4, "Marketing"},
  18. { 5, "Risk & Compliance"},
  19. { 6, "Executive"},
  20. { 7, "Legal"},
  21. { 8, "Trading"},
  22. { 9, "Technology"},
  23. { 0, "Other"}
  24. };
  25. /// <summary>
  26. /// JsonElment 转为字符串,若输入为空则输出""
  27. /// </summary>
  28. /// <param name="jsonElement"></param>
  29. /// <returns></returns>
  30. public static string Json2Text(JsonElement jsonElement, string propertyName)
  31. {
  32. string str = "";
  33. JsonElement elm;
  34. if (propertyName == null) return str;
  35. bool hasProperty = jsonElement.TryGetProperty(propertyName, out elm);
  36. if (hasProperty == true && elm.ValueKind != JsonValueKind.Undefined)
  37. {
  38. str = elm.ToString().Trim();
  39. }
  40. return str;
  41. }
  42. public static bool Json2Boolean(JsonElement jsonElement, string propertyName)
  43. {
  44. bool ret = false;
  45. JsonElement elm;
  46. if (propertyName == null) return ret;
  47. bool hasProperty = jsonElement.TryGetProperty(propertyName, out elm);
  48. if (hasProperty == true && elm.ValueKind != JsonValueKind.Undefined)
  49. {
  50. ret = (elm.ValueKind == JsonValueKind.True);
  51. }
  52. return ret;
  53. }
  54. public static DataTable Json2Table(JsonElement jsonElement)
  55. {
  56. DataTable dataTable = new DataTable();
  57. // 如果解析出的是数组
  58. if (jsonElement.ValueKind == JsonValueKind.Array)
  59. {
  60. // 遍历数组元素
  61. foreach (JsonElement element in jsonElement.EnumerateArray())
  62. {
  63. // 如果是第一次遍历,元素包含列定义信息,需要创建列
  64. if (dataTable.Columns.Count == 0)
  65. {
  66. // 遍历第一个对象的属性来创建列
  67. foreach (JsonProperty property in element.EnumerateObject())
  68. {
  69. dataTable.Columns.Add(property.Name);
  70. }
  71. }
  72. // 创建新行并填充数据
  73. DataRow newRow = dataTable.NewRow();
  74. foreach (JsonProperty property in element.EnumerateObject())
  75. {
  76. newRow[property.Name] = property.Value.ToString();
  77. }
  78. dataTable.Rows.Add(newRow);
  79. }
  80. }
  81. return dataTable;
  82. }
  83. public static List<Dictionary<string, object>> DataTable2List(DataTable dataTable)
  84. {
  85. if (dataTable == null) return null;
  86. var records = new List<Dictionary<string, object>>();
  87. foreach (DataRow row in dataTable.Rows)
  88. {
  89. var record = new Dictionary<string, object>();
  90. foreach (DataColumn col in dataTable.Columns)
  91. {
  92. // 处理DBNull值
  93. record[col.ColumnName] = row[col] == DBNull.Value ? null : row[col];
  94. }
  95. records.Add(record);
  96. }
  97. return records;
  98. }
  99. }
  100. }