简体   繁体   中英

Javascript Escape from Json.Net

The Json.NET homepage says the project started out as :

a couple of static methods for escaping JavaScript strings

Do these methods still exist? I'm aware that JavaScriptSerializer can do this, but I'm after a one liner to do this.

SerializeObject can serialize a single string (which escapes it) but it returns the quotes

JsonConvert.SerializeObject(@"hi bud's  \no way\");

Returns: "hi bud's \\\\no way\\\\"

I found a nice one-liner solution, but it has only been in the framework since 4.0. Would be nice to have something similar in Json.NET I can use on < 4.0 projects.

HttpUtility.JavaScriptStringEncode example:

HttpUtility.JavaScriptStringEncode(@"hi bud's  \no way\")

Returns: hi bud\'s \\\\no way\\\\

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM