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

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

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

        反序列化列表&lt;AbstractClass&gt;使用 newtonsoft.json

        Deserialize a Listlt;AbstractClassgt; with newtonsoft.json(反序列化列表lt;AbstractClassgt;使用 newtonsoft.json)
      2. <small id='DZEPa'></small><noframes id='DZEPa'>

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

              <bdo id='DZEPa'></bdo><ul id='DZEPa'></ul>

                  <tbody id='DZEPa'></tbody>

              1. <tfoot id='DZEPa'></tfoot>
                  本文介绍了反序列化列表&lt;AbstractClass&gt;使用 newtonsoft.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在尝试序列化和反序列化 abstract 类列表(mustinherit for vb),显然其中只有派生类的实例.

                  i'm trying to serialize and deserialize a list of abstract classes (mustinherit for vb), obviusly inside it there are only instances of derived classes.

                  我用 JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto) 修饰了列表参数,获得如下所示的输出:

                  I've decorated the list parameter with the JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto) obtaining an output that look like this:

                  但是当我反序列化它时一直说他不能反序列化抽象类.

                  But when i deserialize it keep saying that he cannot deserialize an abstract class.

                  http://james.newtonking.com/json/help/index.html?topic=html/SerializeTypeNameHandling.htm

                  public class ConcreteClass
                  {
                      private ObservableCollection<AbstractClass> _Nodes = new ObservableCollection<AbstractClass>();
                      //<Newtonsoft.Json.JsonProperty(itemtypenamehandling:=Newtonsoft.Json.TypeNameHandling.Auto)>
                      public ObservableCollection<AbstractClass> Nodes {
                          get { return this._Nodes; }
                      }
                      public string Name { get; set; }
                      public int Id { get; set; }
                  }
                  
                  public abstract class AbstractClass
                  {
                      private ObservableCollection<AbstractClass> _Nodes = new ObservableCollection<AbstractClass>();
                      [Newtonsoft.Json.JsonProperty(itemtypenamehandling = Newtonsoft.Json.TypeNameHandling.Auto)]
                      public ObservableCollection<AbstractClass> Nodes {
                          get { return this._Nodes; }
                      }
                  }
                  

                  删除它的注释行!

                  推荐答案

                  确保在反序列化时指定 TypeNameHandling,按照文档:

                  Make sure you specify TypeNameHandling when deserializing, as per the docs:

                  // for security TypeNameHandling is required when deserializing
                  Stockholder newStockholder = JsonConvert.DeserializeObject<Stockholder>(jsonTypeNameAuto, new JsonSerializerSettings
                  {
                      TypeNameHandling = TypeNameHandling.Auto
                  });
                  

                  值得注意的是,文档正在反序列化一个包含抽象类集合的具体类.

                  It is worth noting that the documentation is deserializing a Concrete class that contains a collection of Abstract classes.

                  作为一个实验,尝试创建一个一次性类(具体),该类具有一个包含抽象对象列表的单一属性,并查看是否可以对其进行序列化和反序列化.

                  As an experiment try creating a throw-away class (concrete) that has a single property with your list of abstract objects and see if you can serialize and deserialize that.

                  更新:

                  我刚刚在 LINQPad 中测试了以下代码:

                  I just tested the following code in LINQPad:

                  void Main()
                  {
                      var test = new List<Business>();
                      test.Add(new Hotel { Name = "Hilton", Stars = 5 });
                      test.Add(new Pool { Name = "Big Splash", Capacity = 500 });
                  
                      test.Dump();
                  
                      string json = JsonConvert.SerializeObject(test, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings
                      {
                          TypeNameHandling = TypeNameHandling.All
                      });
                  
                      json.Dump();
                  
                      var businesses = JsonConvert.DeserializeObject<List<Business>>(json, new JsonSerializerSettings
                      {
                          TypeNameHandling = TypeNameHandling.All
                      });
                  
                      businesses.Dump();
                  }
                  
                  // Define other methods and classes here
                  public abstract class Business
                  {
                      public string Name { get;set; }
                  }
                  public class Hotel : Business
                  {
                      public int Stars { get;set; }
                  }
                  public class Pool : Business
                  {
                      public int Capacity { get;set;}
                  }
                  

                  效果很好.抽象集合序列化为:

                  It worked perfectly. Abstract collection serialized to:

                  {
                    "$type": "System.Collections.Generic.List`1[[UserQuery+Business, query_jvrdcu]], mscorlib",
                    "$values": [
                      {
                        "$type": "UserQuery+Hotel, query_jvrdcu",
                        "Stars": 5,
                        "Name": "Hilton"
                      },
                      {
                        "$type": "UserQuery+Pool, query_jvrdcu",
                        "Capacity": 500,
                        "Name": "Big Splash"
                      }
                    ]
                  }
                  

                  原始集合和反序列化集合匹配.

                  The original and the deserialized collections matched.

                  这篇关于反序列化列表&lt;AbstractClass&gt;使用 newtonsoft.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)
                    <i id='Oczt8'><tr id='Oczt8'><dt id='Oczt8'><q id='Oczt8'><span id='Oczt8'><b id='Oczt8'><form id='Oczt8'><ins id='Oczt8'></ins><ul id='Oczt8'></ul><sub id='Oczt8'></sub></form><legend id='Oczt8'></legend><bdo id='Oczt8'><pre id='Oczt8'><center id='Oczt8'></center></pre></bdo></b><th id='Oczt8'></th></span></q></dt></tr></i><div id='Oczt8'><tfoot id='Oczt8'></tfoot><dl id='Oczt8'><fieldset id='Oczt8'></fieldset></dl></div>
                    • <bdo id='Oczt8'></bdo><ul id='Oczt8'></ul>
                    • <tfoot id='Oczt8'></tfoot>
                        <tbody id='Oczt8'></tbody>
                    • <small id='Oczt8'></small><noframes id='Oczt8'>

                          <legend id='Oczt8'><style id='Oczt8'><dir id='Oczt8'><q id='Oczt8'></q></dir></style></legend>