Datasettojson Extension Method

Source: Internet
Author: User

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. data; namespace common {public static class jsonextensions {# region datasettojson Extension Method // <summary> // datasettojson Extension Method /// </Summary> /// <Param name =" DS "> dataset to be imported </param> /// <Param name =" jsonname "> JSON name </param> /// <Param name =" parname "> JSON field name </param> /// <returns> Returns a JSON string </returns> Public Static string datasettojson (This dataset ds, string jsonname, string [] parname) {try {If (DS = NULL) {return "dataset is null, so I can't do it to JSON! ";} If (jsonname. Length <1) {return" you set the JSON name is wrong! ";} If (Ds. Tables [0]. Columns. Count <parname. Length) {return" you give the parname is bigger than dataset columns! ";}String josn =" {"+ jsonname +": ["; string temp =" "; for (Int J = 0; j <Ds. tables [0]. rows. count; j ++) {temp = temp + "{"; for (INT I = 0; I <parname. length; I ++) {temp + = "" + parname [I] + ": \ '" + Ds. tables [0]. rows [J] [parname [I] + "\ '"; if (I! = Parname. length-1) {temp = temp + "," ;}} if (j = Ds. tables [0]. rows. count-1) {temp = temp + "}";} else {temp = temp + "}," ;}} josn = josn + temp + "]}"; return josn;} catch (exception ex) {return "codeing is error ----" + ex. tostring ();}} # endregion # region datasettojson Extension Method // <summary> // datasettojson Extension Method /// </Summary> /// <Param name = "ds"> dataset </param> /// <returns> Returns JSON string </returns> Public static string datasettojson (This dataset DS) {try {If (DS = NULL) {return "dataset is null, so I can't do it to JSON! ";}String josn =" ["; string temp =" "; for (Int J = 0; j <Ds. tables [0]. rows. count; j ++) {temp = temp + "{"; for (INT I = 0; I <Ds. tables [0]. columns. count; I ++) {temp + = "" + Ds. tables [0]. columns [I]. columnname + ": \ '" + Ds. tables [0]. rows [J] [I] + "\ '"; if (I! = Ds. tables [0]. columns. count-1) {temp = temp + "," ;}} if (j = Ds. tables [0]. rows. count-1) {temp = temp + "}";} else {temp = temp + "}," ;}} josn = josn + temp + "]"; return josn;} catch (exception ex) {return "codeing is error ----" + ex. tostring () ;}# endregion }}

 

 

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.