Watch 1 Star 4 Fork 2

猫猫熊 / HospitalErpWinUIC#

Join us
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
Clone or download
JsonHelper.cs 6.03 KB
Copy Edit Web IDE Raw Blame History
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
namespace Dao
{
public class JsonHelper
{
public static Hashtable Json2Hashtable(string json)
{
return JsonConvert.DeserializeObject<Hashtable>(json);
}
public static List<T> Json2ListHashtable<T>(string json)
{
return JsonConvert.DeserializeObject<List<T>>(json);
}
public string toSimpleJson(DataSet ds)
{
Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss'.'fff";
string result = JsonConvert.SerializeObject(ds, timeConverter);
return result;
}
/// <summary>
/// 将字典类型序列化为json字符串
/// </summary>
/// <typeparam name="TKey">字典key</typeparam>
/// <typeparam name="TValue">字典value</typeparam>
/// <param name="dict">要序列化的字典数据</param>
/// <returns>json字符串</returns>
public static string SerializeDictionaryToJsonString<TKey, TValue>(Dictionary<TKey, TValue> dict)
{
if (dict.Count == 0)
return "";
string jsonStr = JsonConvert.SerializeObject(dict);
return jsonStr;
}
/// <summary>
/// 将json字符串反序列化为字典类型
/// </summary>
/// <typeparam name="TKey">字典key</typeparam>
/// <typeparam name="TValue">字典value</typeparam>
/// <param name="jsonStr">json字符串</param>
/// <returns>字典数据</returns>
public static Dictionary<TKey, TValue> DeserializeStringToDictionary<TKey, TValue>(string jsonStr)
{
if (string.IsNullOrEmpty(jsonStr))
return new Dictionary<TKey, TValue>();
Dictionary<TKey, TValue> jsonDict = JsonConvert.DeserializeObject<Dictionary<TKey, TValue>>(jsonStr);
return jsonDict;
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="table"></param>
/// <param name="file"></param>
public static void dataTableToCsv(DataTable table, string file)
{
string title = "";
FileStream fs = new FileStream(file, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(new BufferedStream(fs), System.Text.Encoding.Default);
for (int i = 0; i < table.Columns.Count; i++)
{
title += table.Columns[i].ColumnName + "\t"; //栏位:自动跳到下一单元格
}
title = title.Substring(0, title.Length - 1) + "\n";
sw.Write(title);
foreach (DataRow row in table.Rows)
{
string line = "";
for (int i = 0; i < table.Columns.Count; i++)
{
line += row[i].ToString().Trim() + "\t"; //内容:自动跳到下一单元格
}
line = line.Substring(0, line.Length - 1) + "\n";
sw.Write(line);
}
sw.Close();
fs.Close();
}
/// 将json转换为DataTable
/// </summary>
/// <param name="strJson">得到的json</param>
/// <returns></returns>
public static DataTable ToDataTable(string json)
{
DataTable dataTable = new DataTable(); //实例化
DataTable result;
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
if (arrayList.Count > 0)
{
foreach (Dictionary<string, object> dictionary in arrayList)
{
//if (dictionary.Keys.Count<string>() == 0)
if (dictionary.Keys.Count == 0)
{
result = dataTable;
return result;
}
if (dataTable.Columns.Count == 0)
{
foreach (string current in dictionary.Keys)
{
if (dictionary[current] == null)
{
dataTable.Columns.Add(current, typeof(string));
}
else
{
dataTable.Columns.Add(current, dictionary[current].GetType());
}
dataTable.Columns[current].AllowDBNull = true;
}
}
DataRow dataRow = dataTable.NewRow();
foreach (string current in dictionary.Keys)
{
try
{
dataRow[current] = dictionary[current] == null ? DBNull.Value : dictionary[current];
}
catch (Exception e)
{
//dataTable.Columns[current].AllowDBNull=true;
dataTable.Columns[current].DataType = dictionary[current].GetType();
dataRow[current] = dictionary[current] == null ? DBNull.Value : dictionary[current];
}
}
dataTable.Rows.Add(dataRow);
}
}
result = dataTable;
return result;
}
}
}

Comment ( 0 )

Sign in for post a comment

C#
1
https://gitee.com/maomaoxiong/HospitalErpWinUI.git
git@gitee.com:maomaoxiong/HospitalErpWinUI.git
maomaoxiong
HospitalErpWinUI
HospitalErpWinUI
master

Help Search

182353 aa51c838 1899542 205747 347fc4a1 1899542