Escaping a double quotes in string in c#

后端 未结 1 788
我在风中等你
我在风中等你 2021-01-24 02:26

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

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-24 03:00

    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.

    0 讨论(0)
提交回复
热议问题