Click here to Skip to main content
15,850,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Guys,

I am trying to learn Web api by my own, for the same i have developed very basic Login application in asp.net and middle layar as web api to interact with database.
Everything works fine until the response received at client side. i am converting dataset into JSON in asp.net and at client side(ASP.net application) JSON string which is received is not in proper format(as below).

"{\r\n \"Table\": [\r\n {\r\n \"RESULT\": \"1\"\r\n }\r\n ],\r\n \"Table1\": [\r\n {\r\n \"RESULT\": \"2\"\r\n }\r\n ]\r\n}"


So request you to suggest me a proper solution so that i can received JSON as per below .


"{
"Table":
[
{"RESULT":"1"}
],
"Table1":
[
{"RESULT":"2"}
]
}".


Your precious revert is expected, please do the needful.

Thanks in advance.

What I have tried:

I tried to remove \\ using replace function in c#, but it does not work as per requirement.
Posted
Updated 13-Oct-20 21:54pm
Comments
Richard MacCutchan 18-Jun-19 13:37pm    
Make sure the sending part is formatting it correctly.
F-ES Sitecore 19-Jun-19 5:18am    
Those "\t" type characters aren't actually in the text, it's just the debuggers way of letting you know there is a tab, or a new line etc. As long as you are correctly deserialising your json it will still work.

Your need to read "lines" not straight "text".

Straight text maintains the carriage returns and line feeds; "reading as lines" returns a string collection.

c# - What is the difference between File.ReadAllLines() and File.ReadAllText()? - Stack Overflow[^]
 
Share this answer
 
Hello,

I have faced similar issue where the dataset is being serialized into JSON string and sent as response and at client side it was received in the similar format as in the query. So, I have directly sent the dataset in the response and worked.
Below is what I did.

Set the API ActionMethod return type to
IHttpActionResult

Instead of returning some string, use below
return Ok(YourDataSet);


Most Importantly, set below line in the WebApiConfig.cs file within Register method under App_Start folder...
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new System.Net.Http.Formatting.RequestHeaderMapping("Accept", "text/html", StringComparison.InvariantCultureIgnoreCase, true, "application/json"));


(Above line in config file is used to format the output in JSON result format).

Hope this solution helps to you or someone who faces similar problem.
Kindly correct me in case this is wrong or should not use.
Thank you.
 
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