使用DataContractJsonSerializer knowntype

时间:2016-11-24 10:18:38

标签: c# json

我试图去实现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"
}

1 个答案:

答案 0 :(得分:1)

我在以前使用DataContractJsonSerializer的项目中有类似的经验。似乎“__type”字段对DataContractJsonSerializer具有特殊含义。 这是我使用 Json.net

的主要原因之一

只有它是对象中的第一个属性才有意义,但是如果它有任何其他位置则有效,并且不会抛出异常。你可以试试,但是在Json.txt中改变分支属性下面的__type位置。我不知道是不是这样,你可以找到一个解决方法。 我没有尝试this解决方案,但值得尝试一下:

希望有所帮助