Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello

I have created WCF webservice using c# and it return json format data. My code is like below:

C#
public string SampleService(Stream data)
{
	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\":\"xya@test.com\"}" }

I want output as below:
{ "SampleServiceResult": {"Id":"1","Name":"xyz","email":"xya@test.com"} }

In output it add "\" escape character in output. How can I remove this and return valid json in output.

Thanks

What I have tried:

I have tried to replace "\" but it doesn't work.
Posted
Updated 15-Nov-17 22:22pm
Comments
Karthik_Mahalingam 31-Oct-17 4:15am    
are you using the data in web page (javascript) ?
Rajesh Pandya 31-Oct-17 5:01am    
In IOS and Android
Karthik_Mahalingam 31-Oct-17 5:16am    
its not an issue, it is just an escaping character, it wont impact the data
Rajesh Pandya 31-Oct-17 5:20am    
Yes it won't impact the data..but for parse the json it need to remove the escaping character
Karthik_Mahalingam 31-Oct-17 5:23am    
how are you parsing the data, show the code

1 solution

Hi,

I have got the expected out by applying following changes in my code:

1. Changed return type from String to Stream
2. Replaced the code
return (new JavaScriptSerializer().Serialize(dict));
with
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
return new MemoryStream(Encoding.UTF8.GetBytes(sData));

Thanks everybody for help and support.

Thanks
Rajesh
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900