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.

135 lines
5.3 KiB

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