1

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

9
  • 1
    Where are you seeing those? In the debugger? Commented Dec 7, 2010 at 16:56
  • Yes, in the intermediate window. Could this be the problem? Would the intermediate window be adding the slashes? Commented Dec 7, 2010 at 16:57
  • 1
    That's not what is really returned Commented Dec 7, 2010 at 16:57
  • 1
    "Would the intermediate window be adding the slashes?". Yes, that's exactly what's happening. Add a watch for that value, and view with a Text Viewer, you'll see what's actually in the string, no escapes. Commented Dec 7, 2010 at 17:00
  • 2
    Why Convert.ToChar(32)? What's wrong with \"? I.e. " \"data\": {". Commented Dec 7, 2010 at 17:02

2 Answers 2

5

The QuickWatch/Watch window will add the extra \ in. If you view it in the Text Visualizer, you will not see them:

QuickWatch:

"{ \"data\": { \"urls\": [ {\"url\": \"domain/path1\"} ,{\"url\": \"domain/path2\"} ] }}" 

Visualizer (the actual output):

{ "data": { "urls": [ {"url": "domain/path1"} ,{"url": "domain/path2"} ] }} 

The \ indicates that the quotes have been escaped and will be included in the final string as you're expecting them to be. I.e. there's nothing wrong with your output.

NB. I used "\"" instead of Convert.ToChar(34) when I tested this.

Sign up to request clarification or add additional context in comments.

Comments

3

You may have more luck using the Newtonsoft.JSON library, or alternately just escaping the slashes yourself as \" in your string literals instead of using Char(34).

dataJSON.Append(" \"data\": {"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.