Decode Html-encoded characters during Json deserialization

孤街浪徒 提交于 2019-12-10 15:13:25

问题


I’m using Json.net to deserialize json data received by Web API call. Some fields often have html-encoded characters like " or & How can I have this characters automatically decoded during deserialization?

I came to 2 possible solutions:

  1. Calling System.Web.HttpUtility.HtmlDecode() in property setter like:

    public string Title
    {
        set
        {
            title = System.Web.HttpUtility.HtmlDecode(value);
        }
    }
    
  2. Writing custom JsonConverter that calls System.Web.HttpUtility.HtmlDecode() in ReadJson() method:

    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));
        }
    }
    

But is there any built-in solution that allows to perform html-decoding during json deserialization without additional code?


回答1:


System.Net.WebUtility.HtmlDecode()

or

HttpUtility.HtmlDecode()

is the way to go, nothing built in regarding the JsonSerializer.



来源:https://stackoverflow.com/questions/35973032/decode-html-encoded-characters-during-json-deserialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!