How to remove backslashes from a JSON response in C #?

I wrote a web service that converts the TP-Open Service XML response to JSON format. I am new to WCF and am writing Webservice. But the converted JSON format is displayed as follows.

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

How to remove these backslashes \ 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 let me know if you need more information.

+5
source share
4 answers

I think your JSON is fine, back braids avoid quotes that people talked about. The following code shows the correct XML conversion -> Json. (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 look in debug mode, you will see a backslash, but the result is valid Json.

Output

{"note":{"to":"Tove","from":"Jani","heading":"Reminder","body":"Don't forget me this weekend!"}}
+3
source

, ? , escape-, .

+3
str = JToken.Parse(your string).ToString();
0

: System.Net.WebUtility.HtmlEncode(JsonString);

response time: System.Net.WebUtility.HtmlDecode (JsonString);

if your decoded code contains this line \\", then replace \\with\

0
source

All Articles