I have an aspx page that retrieves some user generated text from the database and passes to JQuery Ajax method as a JSON Object.
我有一个aspx页面,它从数据库中检索一些用户生成的文本,并作为JSON对象传递给JQuery Ajax方法。
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.
它自己的JSON字符串很简单{“popContent”:“
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?
我试图使用Json.NET来逃避这一点。文档是指序列化对象,但不清楚如何转义字符串。这可能与Json.NET一起使用吗?或者我应该用这个字符串创建一个对象并序列化?
Thanks
1 个解决方案
#1
1
It is possible using JSON.NET.
可以使用JSON.NET。
Since you're using .Net 2.0, you don't have anonymous types and cannot do this:
由于您使用的是.Net 2.0,因此您没有匿名类型,并且无法执行此操作:
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.
所以我建议你创建一个具有适当属性的类,然后在属性上设置HTML内容并使用JSON.NET来序列化整个对象。
Something like this:
像这样的东西:
ContentWrapper cw = new ContentWrapper();
cw.PopContent = "<div>html content</div>";
string json = JsonConvert.SerializeObject(cw);
#1
1
It is possible using JSON.NET.
可以使用JSON.NET。
Since you're using .Net 2.0, you don't have anonymous types and cannot do this:
由于您使用的是.Net 2.0,因此您没有匿名类型,并且无法执行此操作:
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.
所以我建议你创建一个具有适当属性的类,然后在属性上设置HTML内容并使用JSON.NET来序列化整个对象。
Something like this:
像这样的东西:
ContentWrapper cw = new ContentWrapper();
cw.PopContent = "<div>html content</div>";
string json = JsonConvert.SerializeObject(cw);