Journeyman
Journeyman

Reputation: 10271

C# build JSON string that has semicolons in the data

I am creating a string as follows:

string title = "Q:hello";

I then want to embed this string in a JSON string to send to the client. How do I escape the semicolon? I don't want to use a library.

Upvotes: 3

Views: 5222

Answers (2)

faester
faester

Reputation: 15076

Try using backslash http://json.org/

Even though you don't want libraries try look at the JavaScriptSerializer http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

its standard .net so you won't add dependencies. (Although it requires a reference to System.Web.Extensions)

Upvotes: 0

Teoman Soygul
Teoman Soygul

Reputation: 25742

According to the spec, only characters that need escaping are ", \, and control-character:

\"
\\
\/
\b
\f
\n
\r
\t

so you don't need to escape semicolon in a Json string. Ref: http://www.json.org/ (on the right side)

Upvotes: 2

Related Questions