Asp.net JSONHelper JSON help class

Source: Internet
Author: User

Copy codeThe Code is as follows:
/*************************************** ***********
* Copyright: Mr_Sheng
* File name: JSONHelper. cs
* File description:
* Type description: JSONHelper JSON help class
* Authorization statement:
* This program is free software;
* You may re-publish and/or modify this program in accordance with the GPL v3 authorization Terms issued by the Free Software Foundation;
* This procedure is released for the purpose of use, but is not subject to any Warranty liability;
* There is also no implied guarantee for the suitability of sales or specific purposes.
* For details, see GNU General Public Authorization v3(see the license.txt file ).
* Version history:
* V2.0.0 Mr_Sheng Modification
**************************************** ***********/
Using System;
Using System. Collections. Generic;
Using System. Web;
Using System. Web. Script. Serialization;
Using System. Data;
Namespace Sheng. Common
{
/// <Summary>
/// JSON help class
/// </Summary>
Public class JSONHelper
{
/// <Summary>
/// Convert the object to JSON
/// </Summary>
/// <Param name = "obj"> Object </param>
/// <Returns> JSON 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 key-value pair set
/// Convert the DataTable into a List set and store each row
/// The set contains the key-Value Pair dictionary, storing each column
/// </Summary>
/// <Param name = "dt"> 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>
/// Array dictionary
/// </Summary>
/// <Param name = "dataSet"> dataSet </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, ableabletolist (dt ));
Return result;
}
/// <Summary>
/// Convert the data table to JSON
/// </Summary>
/// <Param name = "dataTable"> data table </param>
/// <Returns> JSON string </returns>
Public static string DataTableToJSON (DataTable dt)
{
Return ObjectToJSON (DataTableToList (dt ));
}
/// <Summary>
/// Convert JSON text to object, generic Method
/// </Summary>
/// <Typeparam name = "T"> type </typeparam>
/// <Param name = "jsonText"> JSON text </param>
/// <Returns> specified object 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>
/// Convert the JSON text to the data table
/// </Summary>
/// <Param name = "jsonText"> JSON 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>
/// Convert JSON text into data rows
/// </Summary>
/// <Param name = "jsonText"> JSON 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 newly added by. Net 3.5.
If you want to use it in versions earlier than 3.5, you can download System. Web. Extensions. dll in 3.5 to introduce it to your own application.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.