如何从RootObject解析Json?

时间:2014-10-10 11:14:33

标签: c# asp.net json json.net

我正在尝试解析JSON响应,因此我创建了一些类。

其实我想要 Leg Flight 类元素值。我试图从 RootObject 获取这些元素值,但我不知道如何做到这一点。我用谷歌搜索,但我有点迷惑。

我粘贴了我的JSON respose,Classes !!

JSON响应:

http://pastebin.com/fjjLxkd2

课程:

public class Detail
{

}

  public class Airport
 {
      public string kind { get; set; }
   public string code { get; set; }
   public string city { get; set; }
   public string name { get; set; }
    }

   public class City
  {
     public string kind { get; set; }
    public string code { get; set; }
  public string name { get; set; }
  }

  public class Aircraft
{
public string kind { get; set; }
public string code { get; set; }
public string name { get; set; }
}

 public class Tax
 {
public string kind { get; set; }
public string id { get; set; }
public string name { get; set; }
}

  public class Carrier
 {
public string kind { get; set; }
public string code { get; set; }
public string name { get; set; }
}

 public class Data
{
public string kind { get; set; }
public List<Airport> airport { get; set; }
public List<City> city { get; set; }
public List<Aircraft> aircraft { get; set; }
public List<Tax> tax { get; set; }
public List<Carrier> carrier { get; set; }
 }

 public class Flight
 {
public string carrier { get; set; }
public string number { get; set; }
}

 public class Leg
 {
public string kind { get; set; }
public string id { get; set; }
public string aircraft { get; set; }
public string arrivalTime { get; set; }
public string departureTime { get; set; }
public string origin { get; set; }
public string destination { get; set; }
public string originTerminal { get; set; }
public int duration { get; set; }
public int onTimePerformance { get; set; }
public int mileage { get; set; }
public string meal { get; set; }
public bool secure { get; set; }
public string destinationTerminal { get; set; }
public string operatingDisclosure { get; set; }
 }

 public class Segment
{
public string kind { get; set; }
public int duration { get; set; }
public Flight flight { get; set; }
public string id { get; set; }
public string cabin { get; set; }
public string bookingCode { get; set; }
public int bookingCodeCount { get; set; }
public string marriedSegmentGroup { get; set; }
public List<Leg> leg { get; set; }
public int connectionDuration { get; set; }
}

 public class Slouse
 {
public string kind { get; set; }
public int duration { get; set; }
public List<Segment> segment { get; set; }
 }

  public class Fare
 {
public string kind { get; set; }
public string id { get; set; }
public string carrier { get; set; }
public string origin { get; set; }
public string destination { get; set; }
public string basisCode { get; set; }
 }

  public class BagDescriptor
 {
public string kind { get; set; }
public string commercialName { get; set; }
public int count { get; set; }
public string subcode { get; set; }
public List<string> description { get; set; }
  }

 public class FreeBaggageOption
 {
public string kind { get; set; }
public List<BagDescriptor> bagDescriptor { get; set; }
public int pieces { get; set; }
 }

 public class SegmentPricing
 {
public string kind { get; set; }
public string fareId { get; set; }
public string segmentId { get; set; }
public List<FreeBaggageOption> freeBaggageOption { get; set; }
 }

 public class Passengers
{
public string kind { get; set; }
public int adultCount { get; set; }
}

 public class Tax2
{
public string kind { get; set; }
public string id { get; set; }
public string chargeType { get; set; }
public string code { get; set; }
public string country { get; set; }
public string salePrice { get; set; }
}

 public class Pricing
{
public string kind { get; set; }
public List<Fare> fare { get; set; }
public List<SegmentPricing> segmentPricing { get; set; }
public string baseFareTotal { get; set; }
public string saleFareTotal { get; set; }
public string saleTaxTotal { get; set; }
public string saleTotal { get; set; }
public Passengers passengers { get; set; }
public List<Tax2> tax { get; set; }
public string fareCalculation { get; set; }
public string latestTicketingTime { get; set; }
public string ptc { get; set; }
 }

 public class TripOption
{
public string kind { get; set; }
public string saleTotal { get; set; }
public string id { get; set; }
public List<Slouse> slice { get; set; }
public List<Pricing> pricing { get; set; }
}

 public class Trips
{
public string kind { get; set; }
public string requestId { get; set; }
public Data data { get; set; }
public List<TripOption> tripOption { get; set; }
}

public class RootObject
{
public string kind { get; set; }
public Trips trips { get; set; }
}

代码:

     var obj0 = JsonConvert.DeserializeObject<RootObject>(responsedata);

这里我只获得了Trip类元素。我想要Leg和Flight类元素。

1 个答案:

答案 0 :(得分:0)

如果要查找特定分支的信息,则需要使用Linq来遍历树。例如,如果您只知道腿部ID,则可以执行以下操作:

var allLegs = obj0.trips.tripOption.SelectMany(to => to.slice.SelectMany(sl => sl.segment.Select(sg => sg.leg)));

var leg = allLegs.FirstOrDefault(l => l.id == "yourId");

检索航班的查询类似。您还可以按tripOption ID进行过滤,以获取特定的tripOption,然后检索与其相关联的航班或航段。