Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
384 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...