使用 JavaScriptSerializer 将 sql table 转换为字符串值时如何添加页眉和页脚常量
how to add a header and and footer constants when converting a sql table to a string value using JavaScriptSerializer
有人能告诉我怎么做吗?
我在c#中有这个函数:
public string ConvertLocationTableToString()
{
int radius = 0;
string locationType = "marker";
DataTable dt = new DataTable();
Using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()))
{
Using (SqlCommand cmd = new SqlCommand("SELECT lat=Latitude, lng=Longitude, FROM Locations", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
}
}
tableLocations 包含两行 latitude 和 lontitude 值.
It produces string value of:
[{"lat":24.816925048828125,"lng":-107.37641906738281}
, {"lat":24.815664291381836,"lng":-107.38169097900391}]
.
But I want to produce the following:
[{"Coordinates": [{"lat":24.816925048828125,"lng":-107.37641906738281}], "Radius": 0,"LocationType": "marker"}
,{ "Coordinates": [{lat":24.815664291381836,"lng":-107.38169097900391}],"Radius": 0,"LocationType": "marker"}}]
请注意,“Radius”和“LocationType”不是 table 中的字段。
感谢您的帮助。
鲁本克
您正在序列化从 table 返回的行。如果您在 C# 中创建了一个与您想要的输出相匹配的对象,然后遍历返回的行并设置相关项,您就可以对其进行序列化。
在您的情况下,对象将包括:
- 坐标 - 某种类型的列表
- Radius - int 我假设
- LocationType - 猜测枚举
然后您将创建一个数组,并序列化该数组。
有人能告诉我怎么做吗?
我在c#中有这个函数:
public string ConvertLocationTableToString()
{
int radius = 0;
string locationType = "marker";
DataTable dt = new DataTable();
Using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()))
{
Using (SqlCommand cmd = new SqlCommand("SELECT lat=Latitude, lng=Longitude, FROM Locations", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
}
}
tableLocations 包含两行 latitude 和 lontitude 值.
It produces string value of:
[{"lat":24.816925048828125,"lng":-107.37641906738281} , {"lat":24.815664291381836,"lng":-107.38169097900391}]
.
But I want to produce the following:
[{"Coordinates": [{"lat":24.816925048828125,"lng":-107.37641906738281}], "Radius": 0,"LocationType": "marker"} ,{ "Coordinates": [{lat":24.815664291381836,"lng":-107.38169097900391}],"Radius": 0,"LocationType": "marker"}}]
请注意,“Radius”和“LocationType”不是 table 中的字段。
感谢您的帮助。
鲁本克
您正在序列化从 table 返回的行。如果您在 C# 中创建了一个与您想要的输出相匹配的对象,然后遍历返回的行并设置相关项,您就可以对其进行序列化。
在您的情况下,对象将包括: - 坐标 - 某种类型的列表 - Radius - int 我假设 - LocationType - 猜测枚举
然后您将创建一个数组,并序列化该数组。