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?

7
  • All your debug prints are on t1. Commented Jan 9, 2013 at 18:12
  • Is your code sample wrong? Your Debug.Print statements all use t1. Commented Jan 9, 2013 at 18:12
  • 1
    BTW, that's invalid JSON. Commented Jan 9, 2013 at 18:17
  • Are you sure that there's no bug in your JSON parser? Your code looks fine in all three cases. Commented Jan 9, 2013 at 18:17
  • Even when fixed to print each string they all are the same - they still have the escape character Commented Jan 9, 2013 at 18:21

1 Answer 1

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.

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

7 Comments

The problem is that on the server side (Linux) is receives the string with the escape character and doesn't understand it
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.
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.
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.
" I'm building the HTTP packet manually " this is likely where the problem exists. Drop that code on us.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.