• <bdo id='meBEc'></bdo><ul id='meBEc'></ul>

    <small id='meBEc'></small><noframes id='meBEc'>

        <i id='meBEc'><tr id='meBEc'><dt id='meBEc'><q id='meBEc'><span id='meBEc'><b id='meBEc'><form id='meBEc'><ins id='meBEc'></ins><ul id='meBEc'></ul><sub id='meBEc'></sub></form><legend id='meBEc'></legend><bdo id='meBEc'><pre id='meBEc'><center id='meBEc'></center></pre></bdo></b><th id='meBEc'></th></span></q></dt></tr></i><div id='meBEc'><tfoot id='meBEc'></tfoot><dl id='meBEc'><fieldset id='meBEc'></fieldset></dl></div>
      1. <tfoot id='meBEc'></tfoot>
        <legend id='meBEc'><style id='meBEc'><dir id='meBEc'><q id='meBEc'></q></dir></style></legend>
      2. 如何在使用 JSON.Net 进行序列化期间有条件地忽略

        时间:2023-08-23

        • <bdo id='eOEz9'></bdo><ul id='eOEz9'></ul>

                <tbody id='eOEz9'></tbody>
                <i id='eOEz9'><tr id='eOEz9'><dt id='eOEz9'><q id='eOEz9'><span id='eOEz9'><b id='eOEz9'><form id='eOEz9'><ins id='eOEz9'></ins><ul id='eOEz9'></ul><sub id='eOEz9'></sub></form><legend id='eOEz9'></legend><bdo id='eOEz9'><pre id='eOEz9'><center id='eOEz9'></center></pre></bdo></b><th id='eOEz9'></th></span></q></dt></tr></i><div id='eOEz9'><tfoot id='eOEz9'></tfoot><dl id='eOEz9'><fieldset id='eOEz9'></fieldset></dl></div>

                  <tfoot id='eOEz9'></tfoot>

                  <small id='eOEz9'></small><noframes id='eOEz9'>

                  <legend id='eOEz9'><style id='eOEz9'><dir id='eOEz9'><q id='eOEz9'></q></dir></style></legend>
                • 本文介绍了如何在使用 JSON.Net 进行序列化期间有条件地忽略字段和属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何在使用 JSON.Net 进行序列化期间有条件地忽略字段和属性?我不能从 JsonIgnoreAttribute 继承,因为它是一个 sealed 类.我该怎么办?

                  How to Ignoring Fields and Properties Conditionally During Serialization Using JSON.Net? I can't inherit from JsonIgnoreAttribute because it's a sealed class. What should I do?

                  推荐答案

                  我找到了答案.我从 JsonConverter 继承并创建一个新的转换器.

                  I found the answer. I inherit from JsonConverter and create a new convertor.

                  public class CustomJsonConverter : JsonConverter
                  {
                      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                      {
                          var list = (IList)value;
                  
                          JArray s = new JArray();
                  
                          foreach (var item in list)
                          {
                              JToken token = JToken.FromObject(item);
                              JObject obj = new JObject();
                  
                              foreach (JProperty prop in token)
                              {
                                  if (prop.Name != "Title") // your logic here
                                      obj.Add(prop);
                              }
                  
                              s.Add(obj);
                          }
                  
                          s.WriteTo(writer);
                  
                      }
                  
                      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                      {
                          throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
                      }
                  
                      public override bool CanRead
                      {
                          get { return false; }
                      }
                  
                      public override bool CanConvert(Type objectType)
                      {
                          return objectType != typeof(IList);
                      }
                  }
                  

                  这篇关于如何在使用 JSON.Net 进行序列化期间有条件地忽略字段和属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Json.Net:用于自定义命名的 JsonSerializer-Attribute 下一篇:每个请求的 ASP.NET Core API JSON 序列化程序设置

                  相关文章

                  最新文章

                  1. <legend id='LIQYI'><style id='LIQYI'><dir id='LIQYI'><q id='LIQYI'></q></dir></style></legend>
                      <tfoot id='LIQYI'></tfoot>

                      <i id='LIQYI'><tr id='LIQYI'><dt id='LIQYI'><q id='LIQYI'><span id='LIQYI'><b id='LIQYI'><form id='LIQYI'><ins id='LIQYI'></ins><ul id='LIQYI'></ul><sub id='LIQYI'></sub></form><legend id='LIQYI'></legend><bdo id='LIQYI'><pre id='LIQYI'><center id='LIQYI'></center></pre></bdo></b><th id='LIQYI'></th></span></q></dt></tr></i><div id='LIQYI'><tfoot id='LIQYI'></tfoot><dl id='LIQYI'><fieldset id='LIQYI'></fieldset></dl></div>

                        <bdo id='LIQYI'></bdo><ul id='LIQYI'></ul>
                    1. <small id='LIQYI'></small><noframes id='LIQYI'>