1. 程式人生 > >C# json幫助類,JsonHelper

C# json幫助類,JsonHelper

泛型方法 builder per star sda ali 轉換 back 構造

/// <summary>
/// json幫助類
/// </summary>
public class JsonHelper
{
public JsonHelper()
{
//
//TODO: 在此處添加構造函數邏輯
//
}
public static string DataTableToJSON(DataSet dt, string dtName)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter jw = new JsonTextWriter(sw))
{
JsonSerializer ser = new JsonSerializer();
jw.WriteStartObject();
jw.WritePropertyName(dtName);
jw.WriteStartArray();
foreach (DataRow dr in dt.Tables[0].Rows)
{
jw.WriteStartObject();

foreach (DataColumn dc in dt.Tables[0].Columns)
{
jw.WritePropertyName(dc.ColumnName);
ser.Serialize(jw, dr[dc].ToString());
}

jw.WriteEndObject();
}
jw.WriteEndArray();
jw.WriteEndObject();

sw.Close();
jw.Close();
}
return sb.ToString();
}

/// <summary>
/// 對象轉JSON
/// </summary>
/// <param name="obj">對象</param>
/// <returns>JSON格式的字符串</returns>
public static string ObjectToJSON(object obj)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
try
{
return jss.Serialize(obj);
}
catch (Exception ex)
{

throw new Exception("JSONHelper.ObjectToJSON(): " + ex.Message);
}
}

/// <summary>
/// 數據表轉鍵值對集合 www.2cto.com
/// 把DataTable轉成 List集合, 存每一行
/// 集合中放的是鍵值對字典,存每一列
/// </summary>
/// <param name="dt">數據表</param>
/// <returns>哈希表數組</returns>
public static List<Dictionary<string, object>> DataTableToList(DataTable dt)
{
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

foreach (DataRow dr in dt.Rows)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
foreach (DataColumn dc in dt.Columns)
{
dic.Add(dc.ColumnName, dr[dc.ColumnName]);
}
list.Add(dic);
}
return list;
}

/// <summary>
/// 數據集轉鍵值對數組字典
/// </summary>
/// <param name="dataSet">數據集</param>
/// <returns>鍵值對數組字典</returns>
public static Dictionary<string, List<Dictionary<string, object>>> DataSetToDic(DataSet ds)
{
Dictionary<string, List<Dictionary<string, object>>> result = new Dictionary<string, List<Dictionary<string, object>>>();

foreach (DataTable dt in ds.Tables)
result.Add(dt.TableName, DataTableToList(dt));

return result;
}

/// <summary>
/// 數據表轉JSON
/// </summary>
/// <param name="dataTable">數據表</param>
/// <returns>JSON字符串</returns>
public static string DataTableToJSON(DataTable dt)
{
return ObjectToJSON(DataTableToList(dt));
}

/// <summary>
/// JSON文本轉對象,泛型方法
/// </summary>
/// <typeparam name="T">類型</typeparam>
/// <param name="jsonText">JSON文本</param>
/// <returns>指定類型的對象</returns>
public static T JSONToObject<T>(string jsonText)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
try
{
return jss.Deserialize<T>(jsonText);
}
catch (Exception ex)
{
throw new Exception("JSONHelper.JSONToObject(): " + ex.Message);
}
}

/// <summary>
/// 將JSON文本轉換為數據表數據
/// </summary>
/// <param name="jsonText">JSON文本</param>
/// <returns>數據表字典</returns>
public static Dictionary<string, List<Dictionary<string, object>>> TablesDataFromJSON(string jsonText)
{
return JSONToObject<Dictionary<string, List<Dictionary<string, object>>>>(jsonText);
}

/// <summary>
/// 將JSON文本轉換成數據行
/// </summary>
/// <param name="jsonText">JSON文本</param>
/// <returns>數據行的字典</returns>
public static Dictionary<string, object> DataRowFromJSON(string jsonText)
{
return JSONToObject<Dictionary<string, object>>(jsonText);
}
}

C# json幫助類,JsonHelper