I have read countless questions and answers but have yet to see anything that solves the issue.
I am using newtonsoft JSON library and simply want to return some JSON from a table.
So in my controller I simply have (server and db details blanked for privacy):
public IHttpActionResult GetHelloWorld() { string JSONResult; SqlConnection conn = new SqlConnection("Server=<servername>;Database=<dbname>;user=<username>;password=<password>"); DataTable table = new DataTable(); conn.Open(); SqlCommand cmd = new SqlCommand("select name, latitude, longitude from [<schema>].[<tablename>]", conn); using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { adapter.Fill(table); } //System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); //List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>(); //Dictionary<string, object> row; //foreach (DataRow dr in table.Rows) //{ // row = new Dictionary<string, object>(); // foreach (DataColumn col in table.Columns) // { // row.Add(col.ColumnName, dr[col]); // } // rows.Add(row); //} //return Ok(serializer.Serialize(rows),); JSONResult = JsonConvert.SerializeObject(table); return Ok(JSONResult); } Now it returns my result set as expected but I simply cannot get it to return without the double quotes being escaped, so the data looks like this:
"[{\"name\":\"Aaron's Hill\",\"latitude\":51.18276,\"longitude\":-0.63503},{\"name\":\"Abbas Combe\",\"latitude\":51.00113,\"longitude\":-2.42178}, Now many of the answers I have read simply say its because you are looking at the result in the IDE. In my case this is not true, this is the output in the browser window (tried both IE and chrome) and even fiddler complains when you say show me the JSON output it says invalid character at position 3.
You will see I have commented out code which is the other method of returning JSON which I found on here but that still escapes the quotes. Not sure what else to try?