在 C# 中处理 JSON 响应中的转义字符

Pav*_*sky 0 .net c# regex json

我有一个相当简单的问题,出于某种原因,我可以找到使用 SO 和 Google 的帮助。我收到一个 JSON 回复,如下所示:

"{
\"data\": [
    {
        \"type\": \"gif\",
        \"id\": \"FiGiRei2ICzzG\",
        \"url\": \"http: //giphy.com/gifs/funny-cat-FiGiRei2ICzzG\",
        \"bitly_gif_url\": \"http: //gph.is/1fIdLOl\",
        \"bitly_url\": \"http: //gph.is/1fIdLOl\",
        \"embed_url\": \"http: //giphy.com/embed/FiGiRei2ICzzG\",
        \"username\": \"\",
        \"source\": \"http: //tumblr.com\", etc........
Run Code Online (Sandbox Code Playgroud)

所以它是一个标准的 JSON,但带有 \ 转义字符。现在我试图删除这些转义字符以解析 JSON 中的数据。尝试了字符串的 .replace 和其他一些解决方案,但出于某种原因,我仍然使用转义字符..谢谢!这是我用来做请求的代码

 public static void GetRequest()
    {
        string sFullURL = "http://api.giphy.com/v1/gifs/search?q=";
        string sSearchTerm = "funny+cat";
        string sContent;
        string sAPIKey = "&api_key=dc6zaTOxFJmzC";
        string sLimit = "&limit=1";
        string sFullRequest = "http://api.giphy.com/v1/gifs/search?q=funny+cat&api_key=dc6zaTOxFJmzC";
        HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format(sFullURL + sSearchTerm + sAPIKey + sLimit));
        WebReq.Method = "GET";
        HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
        System.Diagnostics.Debug.WriteLine(WebResp.StatusCode);
        System.Diagnostics.Debug.WriteLine(WebResp.Server);
        Stream Answer = WebResp.GetResponseStream();
        StreamReader _Answer = new StreamReader(Answer);
        sContent = _Answer.ReadToEnd();
        sContent = Regex.Replace(sContent, @"\\", ""); 
    }
Run Code Online (Sandbox Code Playgroud)

iml*_*esh 5

看起来您对调试器中的值感到困惑。调试器窗口显示字符串的转义版本。

在此处输入图片说明

您可以单击小放大图标在“文本可视化器”中打开字符串以查看字符串的实际值。

在此处输入图片说明