我得到的是我的json结果,如下所示。我在数据中得到 Back Slashes 。
“[{\”Route No _ \“:\”1001 \“,\”Description \“:\”Testing \“,\”Duration \“:120,\”KM Coverage \“:100.00000000000000000000 ,“里程”:20.50000000000000000000,“免税额”:150.55000000000000000000,“修理和维护”:200.22000000000000000000}]“
我的代码是
public string GetJSONDataForRouteInfo()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(@"Data Source=HITSEDGE\HITSEDGE;Initial Catalog=CCIL1;User ID=dbassist;Password=assist@123"))
{
using (SqlCommand cmd = new SqlCommand("select [Route No_],[Description],[Duration],[KM Coverage],[Mileage],[Allowance],[Repair And Maintenance] from dbo.[CRONUS International Ltd_$Route]", 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);
}
serializer.Serialize(rows);
string resultjson = serializer.Serialize(rows).Replace(@"\", "");
return resultjson;
}
}
}
我的界面是
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetJSONDataForCustmerinfo")]
string GetJSONDataForCustmerinfo();
在这里,我尝试将'\'替换为双重代码'“'。我需要得到我的结果
[{“Route No _”:“1001”,“Description”:“Testing”,“Duration”:120,“KM Coverage”:100.00000000000000000000,“Mileage”:20.50000000000000000000,“Allowance”:150.55000000000000000000, “维修和保养”:200.22000000000000000000}]
如何得到这样的结果。
我替换了我的代码,但是结果相同。
public string GetJSONDataForRouteInfo()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(@"Data Source=HITSEDGE\HITSEDGE;Initial Catalog=CCIL1;User ID=dbassist;Password=assist@123"))
{
using (SqlCommand cmd = new SqlCommand("select [Route No_] as Routeno,[Description],[Duration],[KM Coverage],[Mileage],[Allowance],[Repair And Maintenance] from dbo.[CRONUS International Ltd_$Route]", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
string json = string.Empty; ;
json = JsonConvert.SerializeObject(dt);
string outputjson = json.Replace("\\", "");
return outputjson;
}
}
}
答案 0 :(得分:0)
以这种方式尝试
public string GetJSONDataForRouteInfo()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(@"Data Source=HITSEDGE\HITSEDGE;Initial Catalog=CCIL1;User ID=dbassist;Password=assist@123"))
{
using (SqlCommand cmd = new SqlCommand("select [Route No_] as Routeno,[Description],[Duration],[KM Coverage],[Mileage],[Allowance],[Repair And Maintenance] from dbo.[CRONUS International Ltd_$Route]", 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]);
}
ws.Add(row);
}
return serializer.Serialize(rows);
}
}
}
如果您可以使用Json.NET,则可以执行以下操作
string json = JsonConvert.SerializeObject(dt, Formatting.Indented);