| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- namespace ddq
- {
- public class Utility
- {
- public static readonly Dictionary<int, string> RoleType = new Dictionary<int, string>
- {
- { 1, "Portfolio Manager" },
- { 2, "Researcher"},
- { 3, "Contactor"},
- { 4, "Marketing"},
- { 5, "Risk & Compliance"},
- { 6, "Executive"},
- { 7, "Legal"},
- { 8, "Trading"},
- { 9, "Technology"},
- { 0, "Other"}
- };
- /// <summary>
- /// JsonElment 转为字符串,若输入为空则输出""
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- public static string Json2Text(JsonElement jsonElement, string propertyName)
- {
- string str = "";
- JsonElement elm;
- if (propertyName == null) return str;
- bool hasProperty = jsonElement.TryGetProperty(propertyName, out elm);
- if (hasProperty == true && elm.ValueKind != JsonValueKind.Undefined)
- {
- str = elm.ToString().Trim();
- }
- return str;
- }
- public static bool Json2Boolean(JsonElement jsonElement, string propertyName)
- {
- bool ret = false;
- JsonElement elm;
- if (propertyName == null) return ret;
- bool hasProperty = jsonElement.TryGetProperty(propertyName, out elm);
- if (hasProperty == true && elm.ValueKind != JsonValueKind.Undefined)
- {
- ret = (elm.ValueKind == JsonValueKind.True);
- }
- return ret;
- }
- public static DataTable Json2Table(JsonElement jsonElement)
- {
- DataTable dataTable = new DataTable();
- // 如果解析出的是数组
- if (jsonElement.ValueKind == JsonValueKind.Array)
- {
- // 遍历数组元素
- foreach (JsonElement element in jsonElement.EnumerateArray())
- {
- // 如果是第一次遍历,元素包含列定义信息,需要创建列
- if (dataTable.Columns.Count == 0)
- {
- // 遍历第一个对象的属性来创建列
- foreach (JsonProperty property in element.EnumerateObject())
- {
- dataTable.Columns.Add(property.Name);
- }
- }
- // 创建新行并填充数据
- DataRow newRow = dataTable.NewRow();
- foreach (JsonProperty property in element.EnumerateObject())
- {
- newRow[property.Name] = property.Value.ToString();
- }
- dataTable.Rows.Add(newRow);
- }
- }
- return dataTable;
- }
- public static List<Dictionary<string, object>> DataTable2List(DataTable dataTable)
- {
- if (dataTable == null) return null;
- var records = new List<Dictionary<string, object>>();
- foreach (DataRow row in dataTable.Rows)
- {
- var record = new Dictionary<string, object>();
- foreach (DataColumn col in dataTable.Columns)
- {
- // 处理DBNull值
- record[col.ColumnName] = row[col] == DBNull.Value ? null : row[col];
- }
- records.Add(record);
- }
- return records;
- }
- }
- }
|