如何从C#中的JSON响应中删除反斜杠?

Thi*_*iri 6 c# rest wcf json web-services

我编写了一个Web服务,它将TP-Open服务的XML响应转换为JSON格式.我是WCF的新手并编写Webservice.但转换后的JSON格式显示如下.

"{ \"data\": { \"current_condition\": [ {\"cloudcover\": \"25\", \"humidity\": \"48\", \"observation_time..
Run Code Online (Sandbox Code Playgroud)

到目前为止如何删除这些反斜杠\和我的代码.

   public string checkweather(string q, string num_of_day)
    {
         HttpWebRequest request=..;
               ...
        string Url = string.Format("http://free.worldweatheronline.com/feed/weather.ashx?q={0}&format={1}&num_of_days={2}&key={3}", q, format, num_of_days, key);
        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(Url);
        Request.Method = "GET";

        using (HttpWebResponse response = (HttpWebResponse)Request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {

                using (StreamReader reader = new StreamReader(stream))
                {
                    var result2=reader.ReadToEnd();
         }}}
          return result2;
         }
Run Code Online (Sandbox Code Playgroud)

如果您需要更多信息,请通知我.

Dan*_*mer 3

我认为你的 JSON 很好,反斜杠转义了引号,正如人们所说的。以下代码显示了一些有效的 XML -> Json 转换。(使用Json.NET)

const string xml = "<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>";
XmlDocument node = new XmlDocument();
node.LoadXml(xml);

string json = JsonConvert.SerializeXmlNode(node);
Run Code Online (Sandbox Code Playgroud)

如果在调试模式下查看,您将看到反斜杠,但输出是有效的 Json。

输出

{"note":{"to":"Tove","from":"Jani","heading":"Reminder","body":"Don't forget me this weekend!"}}
Run Code Online (Sandbox Code Playgroud)