Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape JSON string?

Tags:

json

c#

escaping

Are there any classes/functions available to be used for easy JSON escaping? I'd rather not have to write my own.

like image 762
theringostarrs Avatar asked Aug 06 '09 23:08

theringostarrs


People also ask

How do you escape a JSON string in Java?

You can escape String in Java by putting a backslash in double quotes e.g. " can be escaped as \" if it occurs inside String itself. This is ok for a small JSON String but manually replacing each double quote with an escape character for even a medium-size JSON is time taking, boring, and error-prone.

How do you escape a string?

In the platform, the backslash character ( \ ) is used to escape values within strings. The character following the escaping character is treated as a string literal.

Which characters have to be escaped in JSON?

In JSON the only characters you must escape are \, ", and control codes. Thus in order to escape your structure, you'll need a JSON specific function.


1 Answers

I use System.Web.HttpUtility.JavaScriptStringEncode

string quoted = HttpUtility.JavaScriptStringEncode(input); 
like image 200
xmedeko Avatar answered Sep 24 '22 07:09

xmedeko