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 }}