程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> JSON 對象互轉,JSON對象

JSON 對象互轉,JSON對象

編輯:C#入門知識

JSON 對象互轉,JSON對象


 

以前寫過用反射,轉換,後來覺得有很大漏洞,最近發現有人寫過這個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;
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved