I have build a JSON string (to be posted to a web service), and I used the C# StringBuilder class to do this. The problem is, that when I insert quotes, the StringBuilder class escapes them.
I am currently building the JSON string as such:
StringBuilder dataJSON= new StringBuilder(); dataJSON.Append("{"); dataJSON.Append(" " + Convert.ToChar(34) + "data" + Convert.ToChar(34) + ": {"); dataJSON.Append(" " + Convert.ToChar(34) + "urls" + Convert.ToChar(34) + ": ["); dataJSON.Append(" {" + Convert.ToChar(34) + "url" + Convert.ToChar(34) + ": " + Convert.ToChar(34) + domain + "/" + path[0] + Convert.ToChar(34) + "}"); dataJSON.Append(" ,{" + Convert.ToChar(34) + "url" + Convert.ToChar(34) + ": " + Convert.ToChar(34) + domain + "/" + path[1] + Convert.ToChar(34) + "}"); dataJSON.Append(" ]"); dataJSON.Append(" }"); dataJSON.Append("}"); However, the command: dataJSON.ToString(); results in the string:
{ \"data\": { \"urls\": [ {\"url\": \"domain/test1.html\"} , {\"url\": \"domain/test2.html\"} ] }} Notice the escaped quotes? This is really screwing me up, because the server can't handle the slashes.
My desired (which posts fine to my server when I use PHP) should be:
{ "data": { "urls": [ {"url": "domain/test1.html"} , {"url": "domain/test2.html"} ] }} Is there ANY way to get a string in C# to include quotes that will result in the desired string?
Many thanks! Brett
Convert.ToChar(32)? What's wrong with\"? I.e." \"data\": {".