1

I know this has been covered lots of times but I still have a problem with all of the solutions.

I need to build a string to send to a JSON parser which needs quotes in it. I've tried these forms:

string t1 = "[{\"TS\"}]";
string t2 = "[{" + "\"" + "TS" + "\"" + "}]";
string t3 = @"[{""TS""}]"; 
Debug.Print(t1);
Debug.Print(t1);
Debug.Print(t1);

The debug statement shows it correctly [{"TS"}] but when I look at it in the debugger and most importantly when I send the string to my server side json parser is has the escape character in it: "[{\"TS\"}]"

How can I get rid of the escape characters in the actual string?

scoleman2272
  • 358
  • 6
  • 18

1 Answers1

3

The debug statement shows it correctly [{"TS"}] but when I look at it in the debugger and most importantly when I send the string to my server side json parser is has the escape character in it: "[{\"TS\"}]"

From the debugger point of view it will always show the escaped version (this is so you, as the developer, know exactly what the string value is). This is not an error. When you send it to another .Net system, it will again show the escaped version from the debugger point of view. If you output the value, (Response.Write() or Console.WriteLine()) you will see that the version you expect will be there.

If you highlight the variable (from the debugger) and select the dropdown next to the magnifying glass icon and select "Text Visualizer" you will see how it displays in plain text. This may be what you are looking for.

Per your comments, i wanted to suggest that you also watch how you convert your string in to bytes. You want to make sure you encode your bytes in a format that can be understood by other machines. Make sure you convert your string into bytes using a command as follows:

System.Text.Encoding.ASCII.GetBytes(mystring);

I have the sneaking suspicion that you are sending the bit representation of the string itself instead of an encoded version.

Scott Stevens
  • 390
  • 1
  • 7
  • The problem is that on the server side (Linux) is receives the string with the escape character and doesn't understand it – scoleman2272 Jan 09 '13 at 18:50
  • Then either you're not properly inspecting the string on the server side, or your problem is in how the string is sent (which isn't shown) because the code here is just fine. – Servy Jan 09 '13 at 18:51
  • We need some more details then. We need to know how the linux machine is consuming your data. What you have shown us thus far is not incorrect. – Scott Stevens Jan 09 '13 at 19:00
  • On the server side We're looking at the Apache Access logs to see the incoming HTTP data. On the client side I'm building the HTTP packet manually and sending via a socket. – scoleman2272 Jan 09 '13 at 20:17
  • " I'm building the HTTP packet manually " this is likely where the problem exists. Drop that code on us. – Scott Stevens Jan 09 '13 at 20:35
  • Sorry about the formatting, can't figure out how to format code in comments: string t1 = "[{\"TS\"}]"; string RequestData = "GET " + t1; // I add headers here // Opens the connection this._Socket.Connect(); // Sends out the request this._Socket.Send(RequestData); // Fetches the returned data string ResponseData = ""; while (this._Socket.IsConnected || this._Socket.BytesAvailable > 0) ResponseData += this._Socket.Receive(); this._Socket.Close(); – scoleman2272 Jan 09 '13 at 20:46
  • This is not likely the area with the problem. The problem is on the server with how you are sending the data. How are you serializing your string? You likely want to make sure you are doing something like this: "System.Text.Encoding.ASCII.GetBytes()" – Scott Stevens Jan 10 '13 at 19:24