无法反序列化JSON内容

时间:2014-07-15 06:46:16

标签: c# json deserialization json-deserialization

我在堆栈溢出中看到了很多类似的问题。但是,它没有多大帮助。我无法反序列化我的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请求反序列化的值。

2 个答案:

答案 0 :(得分:2)

尝试在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);

应该有效

enter image description here

PS:XmlElement是不必要的,您可以将它们全部删除

答案 1 :(得分:2)

您正在使用DataContractJsonSerializer但不使用DataContract并使用XMLelement属性。

[DataContract]
public class MyClass
{
    [DataMember]
    public string myId { get; set; }

    [DataMember]
    public List<MyClass2> otherClasses { get; set; }
}

[DataContract]
public class MyClass2
{
    [DataMember]
    public string myId { get; set; }

    [DataMember]
    public int someNumber { get; set; }
}

尝试使用以下属性,看看它是否适合您