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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Report.Entity;
using System.IO;
using System.Management;
using Newtonsoft.Json.Linq;
using System.Data;
namespace PeisStart.WInForm.ReportTest
{
public class ClientConfig
{
public static T DeserializeObject<T>(string data) where T : class
{
var obj = JsonConvert.DeserializeObject<T>(data);
return obj;
}
public static string SerializeObject(object data)
{
string str = JsonConvert.SerializeObject(data);
return str;
}
public static string UpdateJson(string data, string insertJson)
{
JObject jsonObject = JObject.Parse(data);
// 将新的 JSON 字符串插入到已有的 JSON 对象中
jsonObject["pic"] = JToken.Parse(insertJson);
// 输出结果
string updatedJson = jsonObject.ToString();
return updatedJson;
}
public static DataSet ConvertJsonToDataSet(string json,string imageUrl,string signUrl)
{
DataSet dataSet = new DataSet();
JObject jobject = JObject.Parse(json);
foreach (var property in jobject.Properties())
{
DataTable dataTable = ConvertToDataTable(property.Name, property.Value as JArray,imageUrl,signUrl);
if (dataTable != null)
{
dataSet.Tables.Add(dataTable);
}
}
return dataSet;
}
private static DataTable ConvertToDataTable(string tableName, JArray jsonArray, string imageUrl,string signUrl)
{
if (jsonArray == null || jsonArray.Count == 0)
return null;
DataTable dataTable = new DataTable(tableName);
// Get the columns from the first JSON object
JObject firstObject = jsonArray[0] as JObject;
if (firstObject != null)
{
foreach (var property in firstObject.Properties())
{
if (property.Name.Contains("Date"))
dataTable.Columns.Add(property.Name, typeof(DateTime));
else
dataTable.Columns.Add(property.Name, typeof(string));
/*
if (property.Name.Equals("displayOrder"))
dataTable.Columns.Add(property.Name, typeof(int));
else if(property.Name.Contains("Date"))
dataTable.Columns.Add(property.Name, typeof(DateTime));
else
dataTable.Columns.Add(property.Name, typeof(string)); // Assuming all values are strings, you might want to infer types dynamically
*/
}
}
// Populate the DataTable with rows
foreach (JObject jsonObject in jsonArray)
{
DataRow row = dataTable.NewRow();
foreach (var property in jsonObject.Properties())
{
if (dataTable.Columns.Contains(property.Name))
{
if (property.Name.Equals("pictureFileName")
|| property.Name.Contains("pictureHorizontal")
) {
var baseUri = new Uri(imageUrl);
var fullUri = new Uri(baseUri, property.Value?.ToString());
row[property.Name] = fullUri.AbsoluteUri;
}
else if (property.Name.ToLower().Contains("url"))
{
if (property.Value != null)
{
var baseUri = new Uri(signUrl);
var fullUri = new Uri(baseUri, property.Value?.ToString());
row[property.Name] = fullUri.AbsoluteUri;
}
}
/*
else if (property.Name.Equals("displayOrder"))
{
if (property.Value == null)
row[property.Name] = 0;
else
row[property.Name] = Convert.ToInt64(property.Value?.ToString());
}*/
else if (property.Name.Contains("Date"))
{
if (property.Value != null)
{
string temp=property.Value.ToString();
if(!string.IsNullOrEmpty(temp) && !temp.Equals("/"))
row[property.Name] = Convert.ToDateTime(temp);
}
}
else
row[property.Name] = property.Value?.ToString(); // Handle null values gracefully
}
}
dataTable.Rows.Add(row);
}
return dataTable;
}
}
}