4

I saw a lot of similar questions in stack overflow. However, it was not of much help. I am having trouble deserializing my JSON. The details are given below:

The JSON content

    {
  "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 Class I created an Entity class to which this should be mapped. It is given below:

 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; }
    }
}

My Attempt Now this is what I am doing to deserialize.:

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);

However, the mapping is not successful. The values in the JSON is not set to entity object. Everything is null.I have been working on this issue for some time now and i am really confused.Would someone tell me what I am doing wrong and what needs to be done to solve the issue?

PS: I need [XMLElement] because I am using the same entity class to store values deserialized from an XML request.

Kenster
  • 23,465
  • 21
  • 80
  • 106
antony.ouseph.k
  • 907
  • 2
  • 15
  • 28
  • 1
    This doesn't answer your question per se, but you might want to look into JSON.net. I always used to use `DataContractJsonSerializer` because I figured it would be too much of a hassle to switch over, but I really do find it was worth the ten minutes it actually wound up taking me. I'm not affiliated in any way, of course, but it's just a lot nicer and easier to use and debug, and allegedly faster too. – Matthew Haugen Jul 15 '14 at 06:55
  • Sure, I will read it right away. Got to find a solution. Thanks – antony.ouseph.k Jul 15 '14 at 06:56

2 Answers2

2

Try add a new class called Root as follow inside EntityTicketingDetailsINReq class.

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

    // other classes omitted
}

Then deserialize it as Root.

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

It should work

enter image description here

PS: The XmlElement is unnecessary, you can remove them all

Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
  • Dont you think "EntityTicketingDetailsINReq" is acting as a root currently ? Is not that enough? – antony.ouseph.k Jul 15 '14 at 07:07
  • @antony.ouseph.k, i don't think its enough, the TicketDetails needs another container. – Yuliam Chandra Jul 15 '14 at 07:08
  • I need the XML element because the same entity class is being used to map an XML request into. I am using 1 entity class for both XML and JSON deserialization – antony.ouseph.k Jul 15 '14 at 07:11
  • 1
    @antony.ouseph.k, the xmlelement can be omitted if the xml element and the property name is the same, you can just use `XmlSerializer` to deserialize it, but cast it to `TicketDetails` instead – Yuliam Chandra Jul 15 '14 at 07:25
2

You're using a DataContractJsonSerializer but not using a DataContract and using XMLelement attributes instead.

[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; }
}

try using the following attributes and see if that works for you

Peter Lea
  • 1,731
  • 2
  • 15
  • 24
  • Oh.. That could actually be it. let me check. The problem is that I am using the same entity class to deserialize an XML request too( 2 in 1). is there a way to make one entity class work for both XML and JSON ? – antony.ouseph.k Jul 15 '14 at 07:03
  • You can read the class using a new DataContractSerializer(typeof(MyClass)); then use an XmlDictionaryWriter. Example here http://stackoverflow.com/questions/11142280/how-to-serialize-deserialize-a-c-sharp-wcf-datacontract-to-from-xml – Peter Lea Jul 15 '14 at 08:09