0

I have an aspx page that retrieves some user generated text from the database and passes to JQuery Ajax method as a JSON Object.

The JSON string it self is simple {"popContent":"<div>html content</div>"}.
The content may have elements such as single quotes, double quotes, carriage returns etc. The problem is as I'm using .net framework 2.0, struggling to find a method that would escape these elements.

I have tried to use Json.NET to escape this. The documentation refers to serializing objects, but not clear on how to escape a string. Is this possible with Json.NET? Or should I create an object with this string and serialize that?

Thanks

1 Answer 1

1

It is possible using JSON.NET.

Since you're using .Net 2.0, you don't have anonymous types and cannot do this:

var result = new {
  popContent = "<div>html content</div>"
};

So I suggest you create a class that has the appropriate properties, then set the HTML content on the property and use JSON.NET for serialize the entire object.

Something like this:

ContentWrapper cw = new ContentWrapper();
cw.PopContent = "<div>html content</div>";
string json = JsonConvert.SerializeObject(cw);
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think 'JsonConvert' is in JSON.NET 2.0. I did the following and worked fine. string json = JavaScriptConvert(myObject);. Thanks

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.