C# 在Json反序列化期间解码Html编码字符
我正在使用Json.net对Web API调用接收的Json数据进行反序列化。某些字段通常具有html编码的字符,如C# 在Json反序列化期间解码Html编码字符,c#,json,serialization,json.net,html-encode,C#,Json,Serialization,Json.net,Html Encode,我正在使用Json.net对Web API调用接收的Json数据进行反序列化。某些字段通常具有html编码的字符,如“或&;如何在反序列化过程中自动解码这些字符 我得出了两种可能的解决方案: 在属性设置器中调用System.Web.HttpUtility.HtmlDecode(),如: public string Title { set { title = System.Web.HttpUtility.HtmlDecode(value); } }
“
或&;
如何在反序列化过程中自动解码这些字符
我得出了两种可能的解决方案:
System.Web.HttpUtility.HtmlDecode()
,如:
public string Title
{
set
{
title = System.Web.HttpUtility.HtmlDecode(value);
}
}
ReadJson()
方法中编写调用System.Web.HttpUtility.HtmlDecode()
的自定义JsonConverter:
public class HtmlEncodingConverter : Newtonsoft.Json.JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(String);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return System.Web.HttpUtility.HtmlDecode((string)reader.Value);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteRawValue(System.Web.HttpUtility.HtmlEncode((string)value));
}
}
System.Net.WebUtility.HtmlDecode()
或
就是这样,没有关于JsonSerializer的内置内容
HttpUtility.HtmlDecode()