I have created a WCF webservice returning json format data. My code is like below:
String sJSONdata = "";
StreamReader reader = new StreamReader(data);
sJSONdata = reader.ReadToEnd();
//'now convert the JSON into a data table
DataTable dt = GetJSONTable(sJSONdata);
dt.TableName = "Customer";
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (DataRow rs in dt.Rows)
{
dict = new Dictionary<string, string>();
foreach (DataColumn col in dt.Columns)
{
dict.Add(col.ColumnName, rs[col].ToString());
}
}
return (new JavaScriptSerializer().Serialize(dict));
And I get the following output:
{ "SampleServiceResult": "{\"Id\":\"1\",\"Name\":\"xyz\",\"email\":\"[email protected]\"}" }
But I want the output as below:
{ "SampleServiceResult": {"Id":"1","Name":"xyz","email":"[email protected]"} }
In the output it adds "\" escape character. How can I remove this and return valid json?
I have tried to replace "\" but it doesn't work.
Thanks
"SampleServiceResult"and a value of"{\"Id\":\"1\",\"Name\":\"xyz\",\"email\":\"[email protected]\"}"(which is a single string). This suggests your actual problem is that theGetJSONTable()method is returning a single column whereas you appear to expect more than one? as I said above, serialize into a class that matches the data rather than trying to use a datatable unless the data varies by a lot.