Get JSON data code in Asp.net

Source: Internet
Author: User

[Csharp] using System; using System. data; using System. configuration; using System. collections; using System. web; using System. web. security; using System. web. UI; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. web. UI. htmlControls; using System. text; using System. data. sqlClient; public partial class Station_Name_Data: System. web. UI. page {protected string strStation_Name = string. empty; protected void Page_Load (object sender, EventArgs e) {string Factory_Name = Request. form ["FactoryName"]. trim (); SqlConnection conn_StationName = new SqlConnection (); conn_StationName.ConnectionString = System. configuration. configurationManager. connectionStrings ["xnyw.gs"]. connectionString; try {conn_StationName.Open (); SqlCommand cmd_Station = new SqlCommand (); __station.connection = conn_StationName; __station.commandtext = "select station_id, station_Name from station_info inner join factory_info on station_info.factory_id = exist where region = '"+ Factory_Name +"' "; SqlDataAdapter sda = new SqlDataAdapter (pai_station); DataSet ds = new DataSet (); sda. fill (ds); // strCompany_Name + = "["; // for (int I = 0; I <ds. tables [0]. rows. count; I ++) // {// The last line is processed separately. // The format is as follows: // * [{"serviceHall_Name ": "Name 1", "serviceHall_ID": "1" },{ "serviceHall_Name": "name 2", "serviceHall_ID ": "2"}] // * // string ID = ds. tables [0]. rows [I] [0]. toString (). trim (); // string Company_Name = ds. tables [0]. rows [I] [1]. toString (). trim (); // strCompany_Name + = "'{Company_ID': '" + ID + "',"; // strCompany_Name + = "'Company _ name ': '"+ Company_Name +"'}, "; // strCompany_Name = strCompany_Name.Remove (strCompany_Name.Length-1); // strCompany_Name + ="] "; DataTable dt = ds. tables [0]; strStation_Name = DataTableToJson (dt, "rootStationName", 0, dt. rows. count);} catch (Exception ex) {Console. writeLine (ex. message);} finally {conn_StationName.Close ();} HttpContext. current. response. write (strStation_Name); HttpContext. current. response. end () ;}public static string DataTableToJson (DataTable dt, string strTableName, int start, int limit) {StringBuilder jsonBuilder = new StringBuilder (); string head = "{success: true, \ "totalRecordCount \": \ "" + dt. rows. count. toString () + "\", \ "; // jsonBuilder. append ("{\" "); string tmpStr =" "; jsonBuilder. append (strTableName); jsonBuilder. append ("\": ["); if (dt. rows. count> 0) {int endCount = start + limit; if (endCount> dt. rows. count) endCount = dt. rows. count; for (int I = start; I <endCount; I ++) {jsonBuilder. append ("{"); for (int j = 0; j <dt. columns. count; j ++) {jsonBuilder. append ("\" "); jsonBuilder. append (dt. columns [j]. columnName); jsonBuilder. append ("\": \ ""); tmpStr = dt. rows [I] [j]. toString (). trim (); jsonBuilder. append (tmpStr. replace ("\" "," \ ""); jsonBuilder. append ("\", ");} jsonBuilder. remove (jsonBuilder. length-1, 1); jsonBuilder. append ("},");} jsonBuilder. remove (jsonBuilder. length-1, 1);} jsonBuilder. append ("]"); jsonBuilder. append ("}"); return head + jsonBuilder. toString ();}}

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.