C# 使用 JSON.Net 序列化包含撇号的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12042302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serializing strings containing apostrophes with JSON.Net
提问by dodexahedron
I am using JSON.Net as my serializer for a large MVC 3 web application in c# and the Razor view engine. For the initial page load in one view, there is a large amount of JSON dumped inside a script tag using @Html.Raw(JsonConvert.SerializeObject(myObject)).
我使用 JSON.Net 作为我的序列化程序,用于 c# 和 Razor 视图引擎中的大型 MVC 3 Web 应用程序。对于一个视图中的初始页面加载,使用@Html.Raw(JsonConvert.SerializeObject(myObject)).
The problem is that some values of some objects contain apostrophes (think names like O'Brien), which JSON.Net is not escaping or encoding in any way.
问题是某些对象的某些值包含撇号(想想像 O'Brien 这样的名字),JSON.Net 不会以任何方式转义或编码。
It's not an option to pre-encode the values stored in the database because that vastly complicates various other processes.
预先编码存储在数据库中的值不是一种选择,因为这会使其他各种过程变得非常复杂。
Is there a way to force JSON.Net to HTML encode the values of the objects that it serializes, the same way that the built-in JavaScriptSerializer does when you call JavaScriptSerializer.Serialize(myObject)? Or, is there a way to deal with this in the view?
有没有办法强制 JSON.Net 对其序列化的对象的值进行 HTML 编码,与调用时内置的 JavaScriptSerializer 的方式相同JavaScriptSerializer.Serialize(myObject)?或者,有没有办法在视图中处理这个问题?
采纳答案by Pointy
Though there are some cases wherein you might want to drop some JSON into your page as a JavaScript string, or an HTML attribute value, most often what you'd do is simply include it directly into JavaScript source, because JSON is valid JavaScript syntax after all.
尽管在某些情况下您可能希望将一些 JSON 作为 JavaScript 字符串或 HTML 属性值放入您的页面,但大多数情况下您要做的只是将其直接包含在 JavaScript 源中,因为 JSON 是有效的 JavaScript 语法全部。
回答by Pavel Bakshy
You can create custom JsonConverter like this:
您可以像这样创建自定义 JsonConverter:
public class EscapeQuoteConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value.ToString().Replace("'", "\'"));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var value = JToken.Load(reader).Value<string>();
return value.Replace("\'", "'");
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(string);
}
}
To use this only for Name property specify it by attribute:
要仅将此用于 Name 属性,请按属性指定它:
public class Person
{
[JsonConverter(typeof(EscapeQuoteConverter))]
public string Name { get; set; }
}
To apply Converter to all strings use:
要将 Converter 应用于所有字符串,请使用:
JsonConvert.SerializeObject(person, Formatting.Indented, new EscapeQuoteConverter());
回答by Preston S
Use System.Web.HttpUtility.HtmlEncode
使用System.Web.HttpUtility.HtmlEncode
HttpUtility.HtmlEncode(JsonConvert.SerializeObject(myObject))
回答by rrymar
JsonSerializerSettings settings = new JsonSerializerSettings
{
StringEscapeHandling = StringEscapeHandling.EscapeHtml
};
JsonConvert.SerializeObject(obj, settings);

