asp.net JSONHelper JSON help class
- 2020-05-07 19:32:19
- OfStack
/**************************************************
* All rights reserved : Mr_Sheng
* wen a The name : JSONHelper.cs
* The file :
* Type specification : JSONHelper JSON Helper classes
* Authorization statement :
* This program is free software;
* According to the free software foundation GPL v3 License terms, this program is reissued and / Or modification;
* This procedure is issued for the purpose of use without any warranty;
* There is also no implied warranty of merchantability or fitness for a particular purpose.
* Please refer to GNU General public authorization v3 (see license.txt File).
* Version history :
* v2.0.0 Mr_Sheng 2009-09-09 Modify the
***************************************************/
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Script.Serialization;
using System.Data;
namespace Sheng.Common
{
/// <summary>
/// JSON Helper classes
/// </summary>
public class JSONHelper
{
/// <summary>
/// Object to turn JSON
/// </summary>
/// <param name="obj"> object </param>
/// <returns>JSON Formatted string </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>
/// Data table turns the collection of key-value pairs
/// the DataTable into List A collection of , To save every 1 line
/// Set is the dictionary of key-value pairs , To save every 1 column
/// </summary>
/// <param name="dt"> The data table </param>
/// <returns> Hash table array </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>
/// Data set to array dictionary
/// </summary>
/// <param name="dataSet"> The data set </param>
/// <returns> Key-value pair array dictionary </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>
/// Turn the data table JSON
/// </summary>
/// <param name="dataTable"> The data table </param>
/// <returns>JSON string </returns>
public static string DataTableToJSON(DataTable dt)
{
return ObjectToJSON(DataTableToList(dt));
}
/// <summary>
/// JSON Text to object , Generic method
/// </summary>
/// <typeparam name="T"> type </typeparam>
/// <param name="jsonText">JSON The text </param>
/// <returns> Object of the specified type </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>
/// will JSON Text is converted to data table data
/// </summary>
/// <param name="jsonText">JSON The text </param>
/// <returns> Data table dictionary </returns>
public static Dictionary<string, List<Dictionary<string, object>>> TablesDataFromJSON(string jsonText)
{
return JSONToObject<Dictionary<string, List<Dictionary<string, object>>>>(jsonText);
}
/// <summary>
/// will JSON Text is converted into data rows
/// </summary>
/// <param name="jsonText">JSON The text </param>
/// <returns> Data row dictionary </returns>
public static Dictionary<string, object> DataRowFromJSON(string jsonText)
{
return JSONToObject<Dictionary<string, object>>(jsonText);
}
}
}
The System.Web.Script.Serialization namespace is a new addition to.Net 3.5.
If you want to used in the following version 3.5, 3.5 can be downloaded in System. Web. Extensions. dll into their own applications.