Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Remove Back Slashes from JSON Response in C#?

I have written a webservice which converts the XML Response of a TP-Open Service into JSON format. I am new to WCF and writing Webservice. But the converted JSON format displays as follow.

"{ \"data\": { \"current_condition\": [ {\"cloudcover\": \"25\", \"humidity\": \"48\", \"observation_time..

How to Remove these back slashes \ and my code so far.

   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;
         }

Please inform me if you need more information.

like image 815
Thiri Avatar asked Feb 02 '13 10:02

Thiri


2 Answers

I think that your JSON is fine, the backslashes are escaping the quotations, which people have said. The following code shows some valid XML -> Json converting. (Using 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);

If you view in debug mode, you will see the backslashes, but the output is valid Json.

Output

{"note":{"to":"Tove","from":"Jani","heading":"Reminder","body":"Don't forget me this weekend!"}}
like image 87
Dan Saltmer Avatar answered Oct 12 '22 23:10

Dan Saltmer


Are you sure there are backslashes in your string? It looks like me they are the escape characters because there are " characters in your string.

like image 23
Maurice Avatar answered Oct 12 '22 23:10

Maurice