Thiri Thiri - 4 months ago 272
JSON Question

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.

Answer

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!"}}