0

In my code I must return Json string, but I can't use HtmlEncode to remove illegal character in MVC2. How can I encode a string to remove illegal JSON characters?

3 Answers 3

1

System.Web.HttpUtility.HtmlEncode() can be used without a HttpContext (using one of the existing Json libraries/classes mentioned in the other answers is probably best, but if you must do what you're asking for, then that's the method you can use)

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

Comments

0

Use the Json.NET open-source library to do your JSON reading/writing

Or you can use the built-in DataContractJsonSerializer

Comments

0

or use the embedded one http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx you are probably building your own json string. in that case, you'll need to implement different kinds of escaping too, not just removing illegal characters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.