jquery converts DataTable and list<> data into JSON data

Source: Internet
Author: User
Tags json tojson

The code is as follows Copy Code

General Handler Jsondate.ashx








public void ProcessRequest (HttpContext context)





{





Context. Response.ContentType =


"Text/plain"


;





DataTable data2 = MyData ();





String str = Datatabletojson (


"Myjson"


, data2);


Built JSON data





context.htm = htm&amp; (str);





}





public bool IsReusable





{





Get





{





Return





False


;





}





}





Using a DataTable to do data, data simulation





Public DataTable MyData ()





{





DataTable dt =


New





DataTable ();





DataColumn DC;





DataRow Dr;





DC =


New





DataColumn (


"Name"


, System.Type.GetType (


"System.String"


));





Dt. Columns.Add (DC);





DC =


New





DataColumn (


"Email"


, System.Type.GetType (


"System.String"


));





Dt. Columns.Add (DC);





DC =


New





DataColumn (


"Phon"


, System.Type.GetType (


"System.String"


));





Dt. Columns.Add (DC);





DC =


New





DataColumn (


"Move"


, System.Type.GetType (


"System.String"


));





Dt. Columns.Add (DC);





DC =


New





DataColumn (


"Pan"


, System.Type.GetType (


"System.String"


));





Dt. Columns.Add (DC);





DC =


New





DataColumn (


"Select"


, System.Type.GetType (


"System.String"


));





Dt. Columns.Add (DC);





For





(int i = 1; i &lt; 7; i++)





{





Dr = dt. NewRow ();





dr[


"Name"


] =


"Name"





+ i.tostring ();





dr[


"Email"


] =


"Email"





+ i.tostring ();





dr[


"Phon"


] =


"Phon"





+ i.tostring ();





dr[


"Move"


] =


"Move"





+ i.tostring ();





dr[


"Pan"


] =


"Pan"





+ i.tostring ();





dr[


"Select"


] = i.ToString ();





Dt. Rows.Add (DR);





}





int count = dt. Rows.Count;





Return





Dt





}





Converting DataTable data to JSON data





public string Datatabletojson (string jsonname, DataTable DT)





{





StringBuilder Json =


New





StringBuilder ();





Json.append (


"["


);





If





(dt. Rows.Count &gt; 0)





{





For





(int i = 0; i &lt; dt.) Rows.Count; i++)





{





Json.append (


"{"


);





For





(int j = 0; j &lt; dt.) Columns.count; J + +)





{





Json.append (dt. COLUMNS[J]. Columnname.tostring () +


":""





+ dt. ROWS[I][J]. ToString () +


"""


);





If





(J &lt; dt.) COLUMNS.COUNT-1)





{





Json.append (


","


);





}





}





Json.append (


"}"


);





If





(i &lt; dt.) ROWS.COUNT-1)





{





Json.append (


","


);





}





}





}





Json.append (


"]"


);





Return





Json.tostring ();





}





To convert list data to JSON data





public string objecttojson&lt;t&gt; (string jsonname, ilist&lt;t&gt; IL)





{





StringBuilder Json =


New





StringBuilder ();





Json.append (


"["


);





If





(IL. Count &gt; 0)





{





For





(int i = 0; i &lt; IL.) Count; i++)





{





T obj = activator.createinstance&lt;t&gt; ();





Type type = obj. GetType ();





propertyinfo[] PiS = type. GetProperties ();





Json.append (


"{"


);





For





(int j = 0; J &lt; PiS.) Length; J + +)





{





Json.append (Pis[j]. Name.tostring () +


":""





+ Pis[j]. GetValue (Il[i],


Null


) +


"""


);





If





(J &lt; PiS.) LENGTH-1)





{





Json.append (


","


);





}





}





Json.append (


"}"


);





If





(I &lt; IL. COUNT-1)





{





Json.append (


","


);





}





}





}





Json.append (


"]"


);





Return





Json.tostring ();





}




Page Default.aspx








function





GetData ()





{





$.getjson (





"Jsondata.ashx"


,


Server-side pages that generate JSON data





function


(JSON)





{





For


(


Var





i=0;i&lt;json.length;i++)





{





Begin





Var





Index=-1;





Var





Txttrlastindex = Findobj (


"Txttrlastindex"


, document);





Var





RowID = parseint (Txttrlastindex.value);





Var





Signframe = Findobj (


"Signframe"


, document);





Add rows





Var





NEWTR = Signframe.insertrow (signFrame.rows.length);





Newtr.id =


"Signitem"





+ RowID;





Add Column: Ordinal





Var





Newnametd=newtr.insertcell (0);





Add Column Contents





newnametd.innerhtml = NewTR.rowIndex.toString ();





Add Column: Name





Var





Newnametd=newtr.insertcell (1);





Add Column Contents





newnametd.innerhtml =


"&lt;input name= ' Txtname"





+ RowID +


"' Id= ' txtname"





+ RowID +


"' Value= '"


+json[i]. name+


"' type= ' text ' size= '/&gt;"


;





Adding columns: E-mail





Var





Newemailtd=newtr.insertcell (2);





Add Column Contents





newemailtd.innerhtml =


"&lt;input name= ' Txtemail"





+ RowID +


"' Id= ' Txtemail"





+ RowID +


"' Value= '"


+json[i]. email+


"' type= ' text ' size= '/&gt;"


;





Add Column: Phone





Var





Newteltd=newtr.insertcell (3);





Add Column Contents





newteltd.innerhtml =


"&lt;input name= ' Txttel"





+ RowID +


"' Id= ' Txttel"





+ RowID +


"' Value= '"


+json[i]. phon+


"' type= ' text ' size= '/&gt;"


;





Add Column: Mobile phone





Var





Newmobiletd=newtr.insertcell (4);





Add Column Contents





newmobiletd.innerhtml =


"&lt;input name= ' Txtmobile"





+ RowID +


"' Id= ' Txtmobile"





+ RowID +


"' Value= '"


+json[i]. Move+


"' type= ' text ' size= '/&gt;"


;





Add Column: Company name





Var





Newcompanytd=newtr.insertcell (5);





Add Column Contents





newcompanytd.innerhtml =


"&lt;input name= ' Txtcompany"





+ RowID +


"' Id= ' Txtcompany"





+ RowID +


"' Value= '"


+json[i]. pan+


"' type= ' text ' size= '/&gt;"


;











Add drop down box





Var





Newcompanytd=newtr.insertcell (6);





newcompanytd.innerhtml =


"&lt;select id= ' select"


+rowid+


"' &gt;&lt;option value= ' 1 ' &gt;1&lt;/option&gt;&lt;option value= ' 2 ' &gt;2&lt;/option&gt;&lt;option value= ' 3 ' &gt;3 &lt;/option&gt;&lt;option value= ' 4 ' &gt;4&lt;/option&gt;&lt;option value= ' 5 ' &gt;5&lt;/option&gt; &lt;/select&gt; '


;











Select the Drop-down box dynamically





For


(


Var





J=0;j&lt;document.getelementbyid (


"Select"


+ROWID). options.length;j++)





{





If


(document.getElementById (


"Select"


+ROWID). Options[j].text==json[i].select)





Index=j;





}





If


(index&gt;=0)





document.getElementById (


"Select"


+ROWID). options[index].selected=


True


;

















Add Column: Delete button





Var





Newdeletetd=newtr.insertcell (7);





Add Column Contents





newdeletetd.innerhtml =


"&lt;div align= ' center ' style= ' width:40px ' &gt;&lt;a href= ' javascript:; ' onclick= ' Deletesignrow (' Signitem ')





+ RowID +


"')" &gt; Delete &lt;/a&gt;&lt;/div&gt; "


;





Push line number to next line





Txttrlastindex.value = (RowID + 1). ToString ();





}





}

















)





}


The transformation of DataTable and JSON

1 Converting a DataTable or ilist<> to JSON format

Using System;


Using System.Data;


Using System.Text;


Using System.Collections.Generic;


Using System.Reflection;


&lt;summary&gt;


Convert a DataTable or ilist&lt;&gt; into JSON format


&lt;/summary&gt;


public class Tojson


{





Public Tojson ()





{





}


The DataTable turns into JSON





public static string Datatabletojson (String jsonname, DataTable DT)





{





StringBuilder Json =


New





StringBuilder ();





Json.append (


"{""





+ Jsonname +


"":["


);





If





(dt. Rows.Count &gt; 0)





{





For





(int i = 0; i &lt; dt.) Rows.Count; i++)





{





Json.append (


"{"


);





For





(int j = 0; j &lt; dt.) Columns.count; J + +)





{





Json.append (


"""





+ dt. COLUMNS[J]. Columnname.tostring () +


"":""





+ dt. ROWS[I][J]. ToString () +


"""


);





If





(J &lt; dt.) COLUMNS.COUNT-1)





{





Json.append (


","


);





}





}





Json.append (


"}"


);





If





(i &lt; dt.) ROWS.COUNT-1)





{





Json.append (


","


);





}





}





}





Json.append (


"]}"


);





Return





Json.tostring ();





}


The list turns into JSON





public static string Objecttojson&lt;t&gt; (String jsonname, ilist&lt;t&gt; IL)





{





StringBuilder Json =


New





StringBuilder ();





Json.append (


"{""





+ Jsonname +


"":["


);





If





(IL. Count &gt; 0)





{





For





(int i = 0; i &lt; IL.) Count; i++)





{





T obj = activator.createinstance&lt;t&gt; ();





Type type = obj. GetType ();





propertyinfo[] PiS = type. GetProperties ();





Json.append (


"{"


);





For





(int j = 0; J &lt; PiS.) Length; J + +)





{





Json.append (


"""





+ Pis[j]. Name.tostring () +


"":""





+ Pis[j]. GetValue (Il[i],


Null


) +


"""


);





If





(J &lt; PiS.) LENGTH-1)





{





Json.append (


","


);





}





}





Json.append (


"}"


);





If





(I &lt; IL. COUNT-1)





{





Json.append (


","


);





}





}





}





Json.append (


"]}"


);





Return





Json.tostring ();





}


}

Thank you webmaster original from: http://www.suchso.com/projecteactual/jquery-datatable-list-json.html

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.