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.

144 lines
5.4 KiB

1 month ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Http;
  6. using System.Threading.Tasks;
  7. using Newtonsoft.Json;
  8. using Report.Entity;
  9. using System.IO;
  10. using System.Management;
  11. using Newtonsoft.Json.Linq;
  12. using System.Data;
  13. using System.Text.RegularExpressions;
  14. using System.Xml.Linq;
  15. using System.Reflection;
  16. using System.Windows.Forms;
  17. namespace ReportLibrary
  18. {
  19. public class ClientConfig
  20. {
  21. public static T DeserializeObject<T>(string data) where T : class
  22. {
  23. var obj = JsonConvert.DeserializeObject<T>(data);
  24. return obj;
  25. }
  26. public static string SerializeObject(object data)
  27. {
  28. string str = JsonConvert.SerializeObject(data);
  29. return str;
  30. }
  31. public static string UpdateJson(string data,string insertJson)
  32. {
  33. JObject jsonObject = JObject.Parse(data);
  34. // 将新的 JSON 字符串插入到已有的 JSON 对象中
  35. jsonObject["pic"] = JToken.Parse(insertJson);
  36. // 输出结果
  37. string updatedJson = jsonObject.ToString();
  38. return updatedJson;
  39. }
  40. public static DataSet ConvertJsonToDataSet(string json, string imageUrl,string signUrl)
  41. {
  42. DataSet dataSet = new DataSet();
  43. JObject jobject = JObject.Parse(json);
  44. foreach (var property in jobject.Properties())
  45. {
  46. DataTable dataTable = ConvertToDataTable(property.Name, property.Value as JArray, imageUrl, signUrl);
  47. if (dataTable != null)
  48. {
  49. dataSet.Tables.Add(dataTable);
  50. }
  51. }
  52. return dataSet;
  53. }
  54. private static DataTable ConvertToDataTable(string tableName, JArray jsonArray, string imageUrl, string signUrl)
  55. {
  56. if (jsonArray == null || jsonArray.Count == 0)
  57. return null;
  58. DataTable dataTable = new DataTable(tableName);
  59. // Get the columns from the first JSON object
  60. JObject firstObject = jsonArray[0] as JObject;
  61. if (firstObject != null)
  62. {
  63. foreach (var property in firstObject.Properties())
  64. {
  65. if (property.Name.Contains("Date"))
  66. dataTable.Columns.Add(property.Name, typeof(DateTime));
  67. else
  68. dataTable.Columns.Add(property.Name, typeof(string));
  69. /*
  70. if (property.Name.Equals("displayOrder"))
  71. dataTable.Columns.Add(property.Name, typeof(int));
  72. else if(property.Name.Contains("Date"))
  73. dataTable.Columns.Add(property.Name, typeof(DateTime));
  74. else
  75. dataTable.Columns.Add(property.Name, typeof(string)); // Assuming all values are strings, you might want to infer types dynamically
  76. */
  77. }
  78. }
  79. // Populate the DataTable with rows
  80. foreach (JObject jsonObject in jsonArray)
  81. {
  82. DataRow row = dataTable.NewRow();
  83. foreach (var property in jsonObject.Properties())
  84. {
  85. if (dataTable.Columns.Contains(property.Name))
  86. {
  87. if (property.Name.ToLower().Equals("picturefilename")
  88. || property.Name.ToLower().Contains("picturehorizontal")
  89. )
  90. {
  91. var baseUri = new Uri(imageUrl);
  92. var fullUri = new Uri(baseUri, property.Value?.ToString());
  93. row[property.Name] = fullUri.AbsoluteUri;
  94. }
  95. else if (property.Name.ToLower().Contains("url"))
  96. {
  97. if (property.Value != null)
  98. {
  99. var baseUri = new Uri(signUrl);
  100. var fullUri = new Uri(baseUri, property.Value?.ToString());
  101. row[property.Name] = fullUri.AbsoluteUri;
  102. }
  103. }
  104. /*
  105. else if (property.Name.Equals("displayOrder"))
  106. {
  107. if (property.Value == null)
  108. row[property.Name] = 0;
  109. else
  110. row[property.Name] = Convert.ToInt64(property.Value?.ToString());
  111. }*/
  112. else if (property.Name.ToLower().Contains("date"))
  113. {
  114. if (property.Value != null)
  115. {
  116. string temp = property.Value.ToString();
  117. if (!string.IsNullOrEmpty(temp) && !temp.Equals("/"))
  118. row[property.Name] = Convert.ToDateTime(temp);
  119. }
  120. }
  121. else
  122. row[property.Name] = property.Value?.ToString(); // Handle null values gracefully
  123. }
  124. }
  125. dataTable.Rows.Add(row);
  126. }
  127. return dataTable;
  128. }
  129. }
  130. }