asp.net jsonhelper JSON help class _ Practical tips

Source: Internet
Author: User
Tags serialization
Copy Code code as follows:

/**************************************************
* All rights reserved: Mr_sheng
* FileName: JSONHelper.cs
* File Description:
* Type description: Jsonhelper JSON help class
* Authorization statement:
* This procedure is free software;
* You may redistribute and/or modify this procedure in accordance with the GPL V3 license Terms issued by the Free Software Foundation;
* This procedure is issued for the purpose of use, but does not bear any liability for security;
* There is no implied warranty of merchantability or fitness for a particular purpose.
* For details please refer to the GNU General Public License V3 (see License.txt file).
* Version history:
* v2.0.0 Mr_sheng 2009-09-09 Revision
***************************************************/
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>
Object turn to JSON
</summary>
<param name= "obj" > Objects </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 key value pair Collection
Turn the DataTable into a list, save each row
The collection is a dictionary of key-value pairs, saving each column
</summary>
<param name= "DT" > Datasheet </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 key value array dictionary
</summary>
<param name= "DataSet" > Data set </param>
<returns> key-value 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>
Data Table Goto JSON
</summary>
<param name= "dataTable" > Datasheet </param>
<returns>json string </returns>
public static string Datatabletojson (DataTable dt)
{
Return Objecttojson (datatabletolist (DT));
}
<summary>
JSON text-object, generic method
</summary>
<typeparam name= "T" > Type </typeparam>
<param name= "Jsontext" >json text </param>
<returns> objects 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>
Convert JSON text to datasheet data
</summary>
<param name= "Jsontext" >json text </param>
<returns> Datasheet 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 to data rows
</summary>
<param name= "Jsontext" >json text </param>
<returns> Dictionary of data rows </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 use it in the 3.5 version below, you can download the System.Web.Extensions.dll from 3.5 and introduce it into your 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.