【发布时间】:2015-08-06 01:21:30
【问题描述】:
我有一个相当简单的问题,由于某种原因,我可以找到使用 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........
所以它是一个标准的 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, @"\\", "");
}
【问题讨论】:
-
显示你试过的代码
-
你在调试器中检查值吗?如果是,则反斜杠不存在。当我在浏览器中打开该网址时,我没有看到任何转义引号。调试器显示字符串的转义版本。可以点击调试器值预览左侧的小放大图标查看实际值
-
我在调试器中看到了这些值。也许它没有显示正确的字符串?
-
哦,我现在看到了,当我通过 System.Diagnostics.Debug.WriteLine(sContent) 运行它时,我可以看到它的格式正确..
-
@PavelZagalsky 很高兴你让它工作......我已经添加了一个答案;)