JSON 对象互转_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > JSON 对象互转

JSON 对象互转

 2015/5/12 12:48:10  奮鬥的.NET  程序员俱乐部  我要评论(0)
  • 摘要:以前写过用反射,转换,后来觉得有很大漏洞,最近发现有人写过这个help类,所以保存下来publicclassJSONHelper{///<summary>///DataRow转JSON///</summary>///<paramname="row">DataRow</param>///<returns>JSON格式对象</returns>publicstaticobjectDataRowToJSON(DataRowrow)
  • 标签:JSON JS

 

以前写过用反射,转换,后来觉得有很大漏洞,最近发现有人写过这个help类,所以保存下来

public class JSONHelper
{
/// <summary>
/// DataRow转JSON
/// </summary>
/// <param name="row">DataRow</param>
/// <returns>JSON格式对象</returns>
public static object DataRowToJSON(DataRow row)
{
Dictionary<string, object> dataList = new Dictionary<string, object>();
foreach (DataColumn column in row.Table.Columns)
{
dataList.Add(column.ColumnName, row[column]);
}

return ObjectToJSON(dataList);
}

/// <summary>
/// DataRow转对象,泛型方法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="row">DataRow</param>
/// <returns>JSON格式对象</returns>
public static T DataRowToObject<T>(DataRow row)
{
return JSONToObject<T>(DataRowToJSON(row).ToString());
}

/// <summary>
/// DataRow转对象,泛型方法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="table">DataTable</param>
/// <returns>JSON格式对象</returns>
public static List<T> DataTableToList<T>(DataTable table)
{
return JSONToList<T>(DataTableToJSON(table).ToString());
}
/// <summary>
/// DataRow转对象,泛型方法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="jsonText">JSON文本</param>
/// <returns>JSON格式对象</returns>
public static List<T> JSONToList<T>(string jsonText)
{
return JSONToObject<List<T>>(jsonText);
}

/// <summary>
/// 对象转JSON
/// </summary>
/// <param name="obj">对象</param>
/// <returns>JSON格式的字符串</returns>
public static object ObjectToJSON(object obj)
{
try
{
JsonSerializerSettings jset = new JsonSerializerSettings();
jset.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
jset.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy'/'MM'/'dd' 'HH':'mm':'ss" });
return JsonConvert.SerializeObject(obj, jset);
}
catch (Exception ex)
{
throw new Exception("JSONHelper.ObjectToJSON(): " + ex.Message);
}
}
/// <summary>
/// 数据表转JSON
/// </summary>
/// <param name="dataTable">数据表</param>
/// <returns>JSON字符串</returns>
public static object DataTableToJSON(DataTable dataTable)
{
return ObjectToJSON(dataTable);
}

/// <summary>
/// JSON文本转对象,泛型方法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="jsonText">JSON文本</param>
/// <returns>指定类型的对象</returns>
public static T JSONToObject<T>(string jsonText)
{
try
{
return JsonConvert.DeserializeObject<T>(jsonText.Replace("undefined", "null"));
}
catch (Exception ex)
{
throw new Exception("JSONHelper.JSONToObject(): " + ex.Message);
}
}

/// <summary>
/// JSON文本转对象
/// </summary>
/// <param name="jsonText">JSON文本</param>
/// <param name="type">类型</param>
/// <returns>指定类型的对象</returns>
public static object JSONToObject(string jsonText, Type type)
{
try
{
return JsonConvert.DeserializeObject(jsonText.Replace("undefined", "null"), type);
}
catch (Exception ex)
{
throw new Exception("JSONHelper.JSONToObject(): " + ex.Message);
}
}


/// <summary>
/// [{column1:1,column2:2,column3:3},{column1:1,column2:2,column3:3}]
/// </summary>
/// <param name="strJson">Json字符串</param>
/// <returns>DataTable</returns>
public static DataTable JSONToDataTable(string strJson)
{
return JsonConvert.DeserializeObject(strJson, typeof(DataTable)) as DataTable;
}
}

上一篇: 天猫国际联手聚划算开新西兰“小窗” 下一篇: 没有下一篇了!
发表评论
用户名: 匿名