编程语言
首页 > 编程语言> > c# – 无法反序列化JSON内容

c# – 无法反序列化JSON内容

作者:互联网

我在堆栈溢出中看到了很多类似的问题.但是,它没有多大帮助.我无法反序列化我的JSON.详情如下:

JSON内容

    {
  "TicketDetails": {

    "General": {
      "TicketingCity": "DEL",
      "ContactNumber": "4567576574",
      "Email": "XXX@example.com"
    },
    "PassengerDetails": {
      "Passengers": {
        "Passenger": [
          {
            "PassengerNum": "1",
            "Title": "MR",
            "LastName": "XXXX",
            "FirstName": "XXXXX",
            "PassType": "ADT",
            "Age": "30",
            "DateOfBirth": "05OCT1983"
          },
          {
            "PassengerNum": "2",
            "Title": "MS",
            "LastName": "XXX",
            "FirstName": "XXX",
            "PassType": "ADT",
            "Age": "19",
            "DateOfBirth": "03JUN1994"
          }
        ]
      }
    },
    "Onward": {
      "Flights": {
        "Flight": [
          {
            "SegmentNum": "1",
            "Vendor": "AI",
            "FltNum": "935",
            "Class": "Y",
            "StartDt": "20141010",
            "StartAirport": "CCJ",
            "EndAirport": "BOM",
            "Status": "NN",
            "StartTm": "1300",
            "EndTm": "1530",
            "DtChg": "00"
          },
          {
            "SegmentNum": "2",
            "Vendor": "AI",
            "FltNum": "935",
            "Class": "Y",
            "StartDt": "20141010",
            "StartAirport": "BOM",
            "EndAirport": "DXB",
            "Status": "NN",
            "StartTm": "1700",
            "EndTm": "2000",
            "DtChg": "00"
          }
        ]
      }
    },
    "ReturnTrip": {
      "Flights": {
        "Flight": [
          {
            "SegmentNum": "1",
            "Vendor": "AI",
            "FltNum": "935",
            "Class": "Y",
            "StartDt": "20141010",
            "StartAirport": "DXB",
            "EndAirport": "BOM",
            "Status": "NN",
            "StartTm": "1300",
            "EndTm": "1530",
            "DtChg": "00"
          },
          {
            "SegmentNum": "2",
            "Vendor": "AI",
            "FltNum": "935",
            "Class": "Y",
            "StartDt": "20141010",
            "StartAirport": "BOM",
            "EndAirport": "CCJ",
            "Status": "NN",
            "StartTm": "1700",
            "EndTm": "2000",
            "DtChg": "00"
          }
        ]
      }
    }
  }
}

实体类
我创建了一个应该映射到它的Entity类.它给出如下:

 public class EntityTicketingDetailsINReq
{
    public class TicketDetails
    {
        [XmlElement("General")]
        public General General { get; set; }
        [XmlElement("PassengerDetails")]
        public PassengerDetails PassengerDetails { get; set; }
        [XmlElement("Onward")]
        public Onward Onward { get; set; }
        [XmlElement("ReturnTrip")]
        public ReturnTrip ReturnTrip { get; set; }
    }
    public class General
    {
        [XmlElement("TicketingCity")]
        public string TicketingCity { get; set; }
        [XmlElement("ContactNumber")]
        public string ContactNumber { get; set; }
        [XmlElement("Email")]
        public string Email { get; set; }
    }
    public class PassengerDetails
    {
        [XmlElement("Passengers")]
        public Passengers Passengers { get; set; }

    }
    public class Passengers
    {
        [XmlElement("Passenger")]
        public List<Passenger> Passenger { get; set; }
    }
    public class Passenger
    {
        [XmlElement("PassengerNum")]
        public string PassengerNum { get; set; }
        [XmlElement("Title")]
        public string Title { get; set; }
        [XmlElement("LastName")]
        public string LastName { get; set; }
        [XmlElement("FirstName")]
        public string FirstName { get; set; }
        [XmlElement("PassType")]
        public string PassType { get; set; }
        [XmlElement("Age")]
        public string Age { get; set; }
        [XmlElement("DateOfBirth")]
        public string DateOfBirth { get; set; }

    }
    public class Onward
    {
        [XmlElement("Flights")]
        public Flights Flights { get; set; }
    }
    public class Flights
    {
        [XmlElement("Flight")]
        public List<Flight> Flight { get; set; }
    }
    public class Flight
    {
        [XmlElement("SegmentNum")]
        public string SegmentNum { get; set; }
        [XmlElement("Vendor")]
        public string Vendor { get; set; }
        [XmlElement("FltNum")]
        public string FltNum { get; set; }
        [XmlElement("Class")]
        public string Class { get; set; }
        [XmlElement("StartDt")]
        public string StartDt { get; set; }
        [XmlElement("StartAirport")]
        public string StartAirport { get; set; }
        [XmlElement("EndAirport")]
        public string EndAirport { get; set; }
        [XmlElement("Status")]
        public string Status { get; set; }
        [XmlElement("StartTm")]
        public string StartTm { get; set; }
        [XmlElement("EndTm")]
        public string EndTm { get; set; }
        [XmlElement("DtChg")]
        public string DtChg { get; set; }

    }

    public class ReturnTrip
    {
        [XmlElement("Flights")]
        public Flights Flights { get; set; }
    }
}

我的尝试
现在这就是我要反序列化的内容:

var Jsonticketdetails = new EntityTicketingDetailsINReq.TicketDetails();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(EntityTicketingDetailsINReq.TicketDetails));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(Jsoncontent));
Jsonticketdetails = (EntityTicketingDetailsINReq.TicketDetails)ser.ReadObject(ms);

但是,映射不成功. JSON中的值未设置为实体对象.一切都是空的.我一直在研究这个问题已经有一段时间了,我真的很困惑.有人会告诉我我做错了什么以及需要做些什么来解决这个问题?

PS:我需要[XMLElement]因为我使用相同的实体类来存储从XML请求反序列化的值.

解决方法:

尝试在EntityTicketingDetailsINReq类中添加一个名为Root的新类.

public class EntityTicketingDetailsINReq
{
    public class Root
    {
        public TicketDetails TicketDetails { get; set; }
    }

    // other classes omitted
}

然后将其反序列化为Root.

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(EntityTicketingDetailsINReq.Root));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(Jsoncontent));
var root = (EntityTicketingDetailsINReq.Root)ser.ReadObject(ms);

它应该工作

PS:XmlElement是不必要的,你可以删除所有

标签:json,c,deserialization,json-deserialization
来源: https://codeday.me/bug/20190624/1278059.html