簡體   English   中英

將 JSON 反序列化為“object”類型的屬性

[英]Deserializing JSON to a property with type “object”

好吧,我正在嘗試將 object 序列化並保存為 JSON 格式,然后反序列化相同的 object 中的 ZD7FDA19FBE7D237 object class如下:

public class NodeModel
{
    public double X { get; set; }
    public double Y { get; set; }
    public int ParentNumber { get; set; }
    public GeometryParams Geometry { get; set; }
    public object Props { get;  set; }
}

我不能使用具體的類而不是 object 作為道具類型,因為不同對象的類型不同。 當我序列化 object 時,我使用派生類型來填充 Props 屬性。 結果在 JSON 文件中結構良好。 但是當我反序列化它時,它會為 Props 屬性返回 null 而其他屬性已成功反序列化。

json 反序列化器無法確定要反序列化到Props的類型。 當您序列化時,它知道類型,因此它按預期進行序列化。

如果您使NodeModel通用:

public class NodeModel<T>
{
   (...)
   public T Props { get;  set; }
}

然后,您可以通過告訴解串器使用什么類型來幫助解串器。

serialiser.DeserialiseObject<NodeModel<SomeType>>(json);

object完成任務

讓我們假設解串器有能力掃描所有可能的類。 即便如此,在很多情況下它也無法做出正確的決定。

考慮以下場景。

public class A
{
    public string Name { get; set; }
    public string Color { get; set; }
}

public class B
{
    public string Name { get; set; }
    public string Color { get; set; }
    public string X { get; set; }
}

public class NodeModel
{
    public object Props { get; set; }
}

public static void Main(string[] args)
{
  var o = new NodeModel { Props = new B() { Name = "I'm B", Color = "Blue", X = null}};

  var json = serialiser.Serialise(o);

  // Json would be something like 
  // {
  //  "Props": {
  //    "Name": "I\u0027m B",
  //    "Color": "Blue",
  //  }
  // }

  //(...)

  var o2 = serialiser.Deserialise(o); 

  // How can the serialiser decide what to deserialise Props to?
  // Is it A or is it B?

}

使用 JSONConvert?

除非我完全誤解了這個問題,否則您希望在這種情況下將 class 中使用的類型設置為屬性“Prop”,否則您會收到添加的 class 類型。 否則我會誤解整個問題。

    public class TestClass
    {
        public string A = "";
    }
    public class NodeModel
    {
        public double X { get; set; }
        public double Y { get; set; }
        public int ParentNumber { get; set; }
        public GeometryParams Geometry { get; set; }
        public object Props { get; set; }
    }
    public class GeometryParams
    {
        public string PropTest { get; set; }
    }

        public void TestMethod()
    {

        var nodeModel = new NodeModel()
        {
            X = 3.5,
            Y = 4.2,
            ParentNumber = 1,
            Geometry = new GeometryParams { PropTest = "value" },
            Props = new TestClass()
        };

        var json = JsonConvert.SerializeObject(nodeModel, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All,  });

        //json result
        //{ 
            //"$type":"WebApplication2.Controllers.ValuesController+NodeModel, WebApplication2",
            //"X":3.5,
            //"Y":4.2,
            //"ParentNumber":1,
            //"Geometry":{ 
                //"$type":"WebApplication2.Controllers.ValuesController+GeometryParams, WebApplication2",
                //"PropTest":"value"},
            //"Props":{ 
                //"$type":"WebApplication2.Controllers.ValuesController+TestClass, WebApplication2",
                //"A":""} 
        //}
        var nodeModel2 = JsonConvert.DeserializeObject<NodeModel>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects });
     }

感謝 Martin https://stackoverflow.com/users/1882699/martin-staufcik的評論將Props的類型更改為JObject幫助我獲得了Props的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM