C # objects go to JSON strings and JSON string to objects

Source: Internet
Author: User

/* Creator: Kitchen knife, the blog of the layman
* Date Created: September 03, 2014
* Function: JSON operation
*
*/

Namespace Net.String.ConsoleApplication
{
Using System;
Using System.Data;
Using System.Collections;
Using System.ComponentModel;
Using System.Collections.Generic;
Using System.Web.Script.Serialization;

    public class Jsonhelper
    {
        //<summary>
       //Convert JSON string to object
       //</summary>
        //<typeparam name= "T" > Object </typeparam>
       //< param name= "Data" >json string </param>
        public static T Deserialize<t> (String data)
        {
             System.Web.Script.Serialization.JavaScriptSerializer JSON = new System.Web.Script.Serialization.JavaScriptSerializer ();
            return JSON. deserialize<t> (data);
       }

<summary>
Turn an object into a JSON string
</summary>
<param name= "O" > Objects </param>
<returns>json string </returns>
public static string Serialize (object o)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder ();
System.Web.Script.Serialization.JavaScriptSerializer JSON = new System.Web.Script.Serialization.JavaScriptSerializer ();
Json. Serialize (o, SB);
Return SB. ToString ();
}

<summary>
Turn a DataTable object into a JSON string
</summary>
public string ToJson (DataTable dt)
{
JavaScriptSerializer JavaScriptSerializer = new JavaScriptSerializer ();
ArrayList ArrayList = new ArrayList ();
foreach (DataRow datarow in dt. Rows)
{
dictionary<string, object> Dictionary = new dictionary<string, object> ();
foreach (DataColumn DataColumn in dt. Columns)
{
Dictionary. ADD (Datacolumn.columnname, Datarow[datacolumn.columnname]);
}
Arraylist.add (dictionary);
}
Return Javascriptserializer.serialize (arrayList);
}
}

public static Class Enumerableextension
{
<summary>
Collection to add an object
</summary>
public static ienumerable<t> add<t> (this ienumerable<t> e, T value)
{
foreach (Var cur in e)
{yield return cur;}
yield return value;
}

<summary>
Turn the collection into a DataTable
</summary>
public static DataTable converttodatatable<t> (this ienumerable<t> enumerable)
{
var datatable = new DataTable ();
foreach (PropertyDescriptor PD in Typedescriptor.getproperties (typeof (T)))
{
DATATABLE.COLUMNS.ADD (PD. Name, PD. PropertyType);
}
foreach (T item in enumerable)
{
var Row = Datatable.newrow ();

foreach (PropertyDescriptor dp in Typedescriptor.getproperties (typeof (T)))
{
ROW[DP. Name] = dp. GetValue (item);
}
DATATABLE.ROWS.ADD (Row);
}
return dataTable;
}
}
}

C # objects go to JSON strings and JSON string to objects

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.