JSON Serializer C#

时间:2013-11-27 08:47:22

标签: c# json serialization

我正在开发在线飞机销售系统。

但我有问题..

网址包含:

string urlFlightSearch = "https://api.iati.com/rest/flightSearch/" + ado.iatiKod + "";

A代码中有“iati.cs”类

public class iati
{
    public class flightSearch
    {
        public string fromAirport { get; set; }
        public bool allinFromCity { get; set; }
        public string toAirport { get; set; }
        public string fromDate { get; set; }
        public string returnDate { get; set; }
        public string adult { get; set; }
        public string currency { get; set; }
    }
    public class Leg
    {
        public string flightNo { get; set; }
        public string aircraft { get; set; }
        public string operatorCode { get; set; }
        public string operatorName { get; set; }
        public string departureAirport { get; set; }
        public string departureTime { get; set; }
        public string departureAirportName { get; set; }
        public string departureCityName { get; set; }
        public string arrivalAirport { get; set; }
        public string arrivalTime { get; set; }
        public string arrivalAirportName { get; set; }
        public string arrivalCityName { get; set; }
    }
    public class Detail
    {
        public double price { get; set; }
        public double serviceFee { get; set; }
        public double tax { get; set; }
        public int suplement { get; set; }
    }
    public class Fare
    {
        public double totalSingleAdultFare { get; set; }
        public string currency { get; set; }
        public string type { get; set; }
        public List<string> segmentNames { get; set; }
        public int freeSeatCount { get; set; }
        public Detail detail { get; set; }
    }
    public class Flight
    {
        public string id { get; set; }
        public string providerKey { get; set; }
        public string pricingType { get; set; }
        public int packageId { get; set; }
        public List<Leg> legs { get; set; }
        public List<Fare> fares { get; set; }
        public int segmentCount { get; set; }
        public int baggage { get; set; }
        public int flightTimeHour { get; set; }
        public int flightTimeMinute { get; set; }
        public int layoverTime { get; set; }
        public bool hasCip { get; set; }
        public bool canBook { get; set; }
        public bool canRezerve { get; set; }
        public bool dayCross { get; set; }
        public bool returnFlight { get; set; }
    }
    public class Result
    {
        public string searchId { get; set; }
        public List<Flight> flights { get; set; }
    }
    public class RootObject
    {
        public Result result { get; set; }
    }
}

并张贴......

WebClient wc = new WebClient();
var serializer = new JavaScriptSerializer();

iati.flightSearch search = new iati.flightSearch()
{
    fromAirport = "IST",
    allinFromCity = true,
    toAirport = "AYT",
    fromDate = "2013-12-23",
    returnDate = "2013-12-30",
    adult = "1",
    currency = "EUR"
};

var serializedResult = serializer.Serialize(search);
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
string result = wc.UploadString(urlFlightSearch, serializedResult);
iati.Flight flight = serializer.Deserialize<iati.Flight>(result);

但是返回的结果总是空着。

See this image here

问候。

1 个答案:

答案 0 :(得分:0)

使用Newtonsoft

JsonConvert.DeserializeObject(result);