在C#中处理JSON响应中的转义字符 [英] Handling escape characters in JSON response in C#

查看:567
本文介绍了在C#中处理JSON响应中的转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题,由于某种原因,我可以找到有关使用SO和Google的帮助. 我收到的JSON回复如下所示:

I have a rather simple issue that for some reason I can find a help for using SO and Google. I am receiving a JSON reply that looks like this:

"{
\"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........

因此它是标准的JSON,但其中加入了\转义字符. 现在,我尝试删除这些转义字符以解析JSON中的数据. 尝试了字符串的.replace和其他解决方案,但是由于某些原因,我仍然使用转义符. 谢谢!! 这是我用来执行请求的代码

So it's a standard JSON but with \ escape characters sprinkled in. Now those escape characters I am trying to remove to parse data from the JSON. Tried the .replace of the string and some other solutions, but for some reason I stay with the escape characters.. Thanks!! This is the code I used to do the request

 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, @"\\", ""); 
    }

推荐答案

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

Looks like you're getting confused by the value in the debugger. The debugger windows shows an escaped version of the string.

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

You can click on the little magnification icon to open the string in a "text visualizer" to see the actual value of the string.

这篇关于在C#中处理JSON响应中的转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆