【问题标题】:Remove slash from Json from server side in asp.net从 asp.net 中的服务器端从 Json 中删除斜线
【发布时间】:2018-11-06 15:55:50
【问题描述】:

我需要从我的输出 json 中删除斜杠。我在我的 API 控制器中使用的以下函数。但我仍然得到了结果。我怎样才能删除这个斜线?

 public string GetEmployeeDetails(string AccessCard)
    {
        DataTable dt =GetEmployeeByAccessCard(AccessCard);
        if (dt.Rows.Count>0)
        {
            string JSONresult;
            JSONresult = JsonConvert.SerializeObject(dt);
            string outputjson = JSONresult.Replace("\\", "");
            return outputjson;

        }
        else
            return "No Data found";

    }

【问题讨论】:

  • 可以参考这个链接并适配正则表达式:forums.asp.net/t/…
  • 它不工作
  • @Vahid 从 json 中删除斜线的任何具体原因?您希望实现哪些特定功能?
  • 我创建了一个 Web API。由于此功能,我需要以正确的格式获取 json。但我得到的是这样的。 "{\"ID\":\"3411\",\"AccessCardNo\":\"123569\",\"DP_EmpID\":\"63\",\"EmpOfficialID\":\"EMP020 \",\"emp.DP_Degree\":\"710\",\"emp.DP_DepartmentID\":\"699\",\"emp.DP_EmpFname\":\"Feras\",\"ActualDepartureDate\" :\"01/01/2099\",\"EmpStatus_HR\":\"保留\"}"
  • 这是网络浏览器向您显示的内容,还是您在调试器中看到的内容?检查此链接也stackoverflow.com/questions/20312974/…

标签: asp.net json asp.net-web-api


【解决方案1】:

在您的json 字符串中有两件事是错误的。 Json" 开头,以" 结尾首先修剪这些双引号。然后替换\\。喜欢

string outputjson=JSONresult.Trim('"');
outputjson = JSONresult.Replace("\\", "");

【讨论】:

    【解决方案2】:

    试试这个代码:

    public string GetEmployeeDetails(string AccessCard)
        {
            DataTable dt =GetEmployeeByAccessCard(AccessCard);
            if (dt.Rows.Count>0)
            {
                string result= Convert.ToString(dt);
                string JSONresult = JsonConvert.SerializeObject(result.Replace("\\", ""));
                return JSONresult;
            }
            else
                return "No Data found";
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-02
      • 2021-02-24
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多