Welcome everyone into QQ Group to discuss:84479667
Here's how: Public Static stringNulldata ="{\ "result\": \ "true\", \ "msg\": \ "no related data \"}"; #regionQuery results converted to JSON/// <summary> ///query results converted to JSON/// </summary> /// <param name= "DT" ></param> /// <param name= "Count" ></param> /// <returns></returns> Public Static stringGetdatatojson (DataTable DT,intCount =0) {JavaScriptSerializer JSS=NewJavaScriptSerializer (); System.Collections.ArrayList DiC=NewSystem.Collections.ArrayList (); if(dt! =NULL&& dt. Rows.Count >0) { if(Count = =0) {Count=dt. Rows.Count; } foreach(DataRow Drinchdt. Rows) {System.Collections.Generic.Dictionary<string,Object> Drow =Newsystem.collections.generic.dictionary<string,Object>(); foreach(DataColumn DCinchdt. Columns) {//string dcvalue = Common.PageBase.delhtml (DR[DC. ColumnName]. ToString ());Drow. ADD (DC. ColumnName, DR[DC. ColumnName]); } dic. ADD (drow); } //bool result = true; stringJsonstring =JSS. Serialize (DIC); stringp =@"\\/date\ ((\d+) \) \\/"; MatchEvaluator MatchEvaluator=NewMatchEvaluator (convertjsondatetodatestring); //matchevaluator img_urlevaluator = new MatchEvaluator (getimgurl);Regex reg =NewRegex (P); //regex img_url = new Regex ("src=\\\\\" (. *?) \""); //regex img_url = new Regex ("Src=\" (. *?) \"");Jsonstring =Reg. Replace (jsonstring, MatchEvaluator); //jsonstring = Img_url. Replace (jsonstring, img_urlevaluator); return "{\ "result\": \ "True\""+","+"\ "Rows\":"+ Count +","+"\ "msg\":"+ jsonstring +"}"; } Else { returnNulldata; } } #endregion /// <summary> ///Convert JSON serialized time from/date (1294499956278+0800) to string/// </summary> /// <param name= "M" ></param> /// <returns></returns> Private Static stringconvertjsondatetodatestring (Match m) {stringresult =string. Empty; DateTime DT=NewDateTime (1970,1,1); DT= dt. Addmilliseconds (Long. Parse (m.groups[1]. Value)); DT=dt. ToLocalTime (); Result= dt. ToString ("YYYY-MM-DD HH:mm:ss"); returnresult; } Public stringCreatejsonparameters (DataTable DT,intCount =0) { if(dt = =NULL|| Dt. Rows.Count = =0) { returnNulldata; } if(Count = =0) {Count=dt. Rows.Count; } BOOLresult =true; StringBuilder jsonstring=NewStringBuilder (); //Exception Handling if(dt! =NULL&& dt. Rows.Count >0) {jsonstring.append ("{ "); Jsonstring.append ("\ "Result\":"+ result +","+"rows:"+ Count +","+"msg:["); for(inti =0; i < dt. Rows.Count; i++) {jsonstring.append ("{ "); for(intj =0; J < dt. Columns.count; J + +) { if(J < dt. Columns.count-1) {jsonstring.append ("\""+ dt. COLUMNS[J]. Columnname.tostring () +"\":"+"\""+ dt. ROWS[I][J]. ToString () +"\","); } Else if(j = = dt. Columns.count-1) {jsonstring.append ("\""+ dt. COLUMNS[J]. Columnname.tostring () +"\":"+"\""+ dt. ROWS[I][J]. ToString () +"\""); } } /*End of String*/ if(i = = dt. Rows.Count-1) {jsonstring.append ("} "); } Else{jsonstring.append ("}, "); }} jsonstring.append ("]}"); returnjsonstring.tostring (); } Else { return NULL; } }
ASP. NET DataSet query results converted to JSON format data