没有反斜杠的 JSON 结果

本文关键字:JSON 结果 | 更新日期: 2023-09-27 18:32:59

我得到我的json结果如下。在这里,我在数据中得到了回斜杠

"[{''"路线No_":''"1001''",''"描述":测试","持续时间":120,"KM覆盖范围":100.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

我的代码是

    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();

在这里,我尝试将"''"替换为双代码"。在这里我需要得到我的结果,比如

[{"路线No_":"1001","描述":"测试","持续时间":120,"KM覆盖范围":100.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

如何得到这样的结果。

我像下面这样替换了我的代码,但结果相同。

 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;
            }
        }
    }

没有反斜杠的 JSON 结果

尝试这样

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);