C# json帮助类,JsonHelper

Posted 许一朵岁月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# json帮助类,JsonHelper相关的知识,希望对你有一定的参考价值。

/// <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的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET(C#) Json序列化反序列化帮助类Jsonhelper

C#序列化及反序列化Json对象通用类JsonHelper

工具类--JsonHelper

此 json 的 C# 类表示

C#公共帮助类 Convert帮助类

C#公共帮助类 Image帮助类