簡體   English   中英

使用DataContractJsonSerializer已知類型

[英]using DataContractJsonSerializer knowntype

我正在嘗試去散布json:

類由json2sharp生成

但這會引發異常:

元素“:數據”包含來自映射到名稱“ http://schemas.datacontract.org/2004/07/TMSoft.CryptoDoc.Gu45:Gu45 ”的數據類型的數據。 反序列化器沒有任何類型的信息,具有相同的名稱。 使用DataContractResolver或添加與“ Gu45”(已知類型的列表)匹配的類型,例如使用KnownTypeAttribute屬性或通過將其添加到傳遞給DataContractSerializer的已知類型的列表中。

如何解決?

namespace json
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = System.IO.File.ReadAllText(@"D:\Json.txt");
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(RootObject));
            var account = serializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(json)));
        }
    }

    public class Branch
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }

    public class Direction
    {
        public object Code { get; set; }
        public string Name { get; set; }
    }

    public class Org
    {
        public string Code { get; set; }
        public string Name { get; set; }
    }

    public class Wagon
    {
        public string Comment { get; set; }
        public object NoteTimeString { get; set; }
        public string Number { get; set; }
        public string OwnerCode { get; set; }
        public string StateString { get; set; }
    }

    public class Data
    {
        public string __type { get; set; }
        public Branch Branch { get; set; }
        public string DeliveryTimeString { get; set; }
        public Direction Direction { get; set; }
        public string EsrCode { get; set; }
        public int GU45Type { get; set; }
        public object ManeuverTime { get; set; }
        public string Number { get; set; }
        public Org Org { get; set; }
        public object ParkName { get; set; }
        public object ParkNum { get; set; }
        public string RailwayName { get; set; }
        public string RegistrationDateTimeString { get; set; }
        public object Span { get; set; }
        public string StationName { get; set; }
        public string TakingTimeString { get; set; }
        public object TrackNum { get; set; }
        public object TrackNumAddition { get; set; }
        public object WagonNote { get; set; }
        public List<Wagon> Wagons { get; set; }
    }

    public class Body
    {
        public Data Data { get; set; }
        public List<int> Signature { get; set; }
    }

    public class RootObject
    {
        public Body Body { get; set; }
        public int Revision { get; set; }
        public int State { get; set; }
        public string StateDate { get; set; }
        public string WosId { get; set; }
    }
}

Json.txt:

{
   "Body": {
      "Data": {
         "__type": "Gu45:#TMSoft.CryptoDoc.Gu45",
         "Branch": {
            "Code": "9898",
            "Name": "Place"
         },
         "DeliveryTimeString": "07.03.2014 10:00:00",
         "Direction": {
            "Code": null,
            "Name": "Test"
         },
         "EsrCode": "320007",
         "GU45Type": 2,
         "ManeuverTime": null,
         "Number": "1",
         "Org": {
            "Code": "1860",
            "Name": "Test"
         },
         "ParkName": null,
         "ParkNum": null,
         "RailwayName": "Test",
         "RegistrationDateTimeString": "07.03.2014",
         "Span": null,
         "StationName": "Test",
         "TakingTimeString": "07.03.2014 12:00:00",
         "TrackNum": null,
         "TrackNumAddition": null,
         "WagonNote": null,
         "Wagons": [
            {
               "Comment": "РЕМТ",
               "NoteTimeString": null,
               "Number": "44916989",
               "OwnerCode": "22",
               "StateString": "0"
            }
         ]
      },
      "Signature": [
         48,
         106
      ]
   },
   "Revision": 1966,
   "State": 2,
   "StateDate": "2014-03-07T12:48:03Z",
   "WosId": "15805729"
}

在以前的DataContractJsonSerializer項目中,我也有類似的經驗。 似乎“ __type”字段對DataContractJsonSerializer具有特殊含義。 這是我使用Json.net的主要原因之一

僅當它是對象中的第一個屬性時才重要,但是如果它具有任何其他位置則它起作用並且不會引發異常。 您可以嘗試一下,但要更改Json.txt中branch屬性下方的__type位置。 我不知道是不是這樣,您可以找到一種解決方法。 我還沒有嘗試過這種解決方案,但是值得嘗試一下:

希望能幫助到你

暫無
暫無

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

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