ASP. NET DataSet query results converted to JSON format data

Source: Internet
Author: User


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

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.