Asp.net下获取JSON数据代码
[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["XNYQFGS"].ConnectionString;
try
{
conn_StationName.Open();
SqlCommand cmd_Station = new SqlCommand();
cmd_Station.Connection = conn_StationName;
cmd_Station.CommandText = "select station_id,Station_Name from station_info inner join factory_info on station_info.factory_id=factory_info.factory_id where factory_info.factory_name='" + Factory_Name + "'";
SqlDataAdapter sda = new SqlDataAdapter(cmd_Station);
DataSet ds = new DataSet();
sda.Fill(ds);
//strCompany_Name += "[";
//for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
//{
// //最后一条单独处理
// //格式如下:
// /*
// * [{"serviceHall_Name":"名字1","serviceHall_ID":"1"},{"serviceHall_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();
}
}
上一篇: 抖音怎么删除购买订单记录?
下一篇: 用PHP去实现数据库查询结果缓存
推荐阅读
-
asp.net提取多层嵌套json数据的方法
-
asp.net DataTable相关操作集锦(筛选,取前N条数据,去重复行,获取指定列数据等)
-
asp.net浏览器向服务器提交json格式的数据
-
总结Visual Studio下ASP.NET模板化控件中的数据绑定
-
android客户端从服务器端获取json数据并解析的实现代码
-
oracle下实现恢复一个丢失的数据文件的代码
-
python结合selenium获取XX省交通违章数据的实现思路及代码
-
从textarea中获取数据之后按原样显示实现代码
-
Android解析json数据示例代码(三种方式)
-
asp.net下获取远程网页的内容之二(downmoon原创)