从嵌套的 Json 中检索值

我有一个 Json,如下所示。


{

  "currency": "MYR",

  "results": [

    {

      "itineraries": [

        {

          "outbound": {

            "flights": [

              {

                "departs_at": "2018-06-03T06:25",

                "arrives_at": "2018-06-03T07:25",

                "origin": {

                  "airport": "PEN"

                },

                "destination": {

                  "airport": "KUL",

                  "terminal": "M"

                },

                "marketing_airline": "OD",

                "operating_airline": "OD",

                "flight_number": "2105",

                "aircraft": "738",

                "booking_info": {

                  "travel_class": "ECONOMY",

                  "booking_code": "Q",

                  "seats_remaining": 9

                }

              }

            ]

          },

          "inbound": {

            "flights": [

              {

                "departs_at": "2018-06-04T14:10",

                "arrives_at": "2018-06-04T15:10",

                "origin": {

                  "airport": "KUL",

                  "terminal": "M"

                },

                "destination": {

                  "airport": "PEN"

                },

                "marketing_airline": "OD",

                "operating_airline": "OD",

                "flight_number": "2108",

                "aircraft": "739",

                "booking_info": {

                  "travel_class": "ECONOMY",

                  "booking_code": "O",

                  "seats_remaining": 5

                }

              }

            ]

          }

        }

      ],

      "fare": {

        "total_price": "360.00",

        "price_per_adult": {

          "total_fare": "360.00",

          "tax": "104.00"

        },

        "restrictions": {

          "refundable": false,

          "change_penalties": true

        }

      }

    }

  ]

}

我使用下面的代码从 Json 中检索值。我能够检索“departs_at”、“arrives_at”、“marketing_airline”,但无法检索“booking_info”中的值。


明月笑刀无情
浏览 176回答 2
2回答

慕丝7291255

从此链接将此 C# 模型用于您的 jsonnamespace Flighting{&nbsp; &nbsp; using System;&nbsp; &nbsp; using System.Collections.Generic;&nbsp; &nbsp; using System.Globalization;&nbsp; &nbsp; using Newtonsoft.Json;&nbsp; &nbsp; using Newtonsoft.Json.Converters;&nbsp; &nbsp; public partial class Flight&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("currency")]&nbsp; &nbsp; &nbsp; &nbsp; public string Currency { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("results")]&nbsp; &nbsp; &nbsp; &nbsp; public List<Result> Results { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public partial class Result&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("itineraries")]&nbsp; &nbsp; &nbsp; &nbsp; public List<Itinerary> Itineraries { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("fare")]&nbsp; &nbsp; &nbsp; &nbsp; public Fare Fare { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public partial class Fare&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("total_price")]&nbsp; &nbsp; &nbsp; &nbsp; public string TotalPrice { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("price_per_adult")]&nbsp; &nbsp; &nbsp; &nbsp; public PricePerAdult PricePerAdult { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("restrictions")]&nbsp; &nbsp; &nbsp; &nbsp; public Restrictions Restrictions { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public partial class PricePerAdult&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("total_fare")]&nbsp; &nbsp; &nbsp; &nbsp; public string TotalFare { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("tax")]&nbsp; &nbsp; &nbsp; &nbsp; public string Tax { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public partial class Restrictions&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("refundable")]&nbsp; &nbsp; &nbsp; &nbsp; public bool Refundable { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("change_penalties")]&nbsp; &nbsp; &nbsp; &nbsp; public bool ChangePenalties { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public partial class Itinerary&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("outbound")]&nbsp; &nbsp; &nbsp; &nbsp; public Outbound Outbound { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("inbound")]&nbsp; &nbsp; &nbsp; &nbsp; public Inbound Inbound { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public partial class Inbound&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("flights")]&nbsp; &nbsp; &nbsp; &nbsp; public List<InboundFlight> Flights { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public partial class InboundFlight&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("departs_at")]&nbsp; &nbsp; &nbsp; &nbsp; public string DepartsAt { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("arrives_at")]&nbsp; &nbsp; &nbsp; &nbsp; public string ArrivesAt { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("origin")]&nbsp; &nbsp; &nbsp; &nbsp; public Origin Origin { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("destination")]&nbsp; &nbsp; &nbsp; &nbsp; public Destination Destination { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("marketing_airline")]&nbsp; &nbsp; &nbsp; &nbsp; public string MarketingAirline { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("operating_airline")]&nbsp; &nbsp; &nbsp; &nbsp; public string OperatingAirline { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("flight_number")]&nbsp; &nbsp; &nbsp; &nbsp; public string FlightNumber { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("aircraft")]&nbsp; &nbsp; &nbsp; &nbsp; public string Aircraft { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("booking_info")]&nbsp; &nbsp; &nbsp; &nbsp; public BookingInfo BookingInfo { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public partial class BookingInfo&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("travel_class")]&nbsp; &nbsp; &nbsp; &nbsp; public string TravelClass { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("booking_code")]&nbsp; &nbsp; &nbsp; &nbsp; public string BookingCode { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("seats_remaining")]&nbsp; &nbsp; &nbsp; &nbsp; public long SeatsRemaining { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public partial class Destination&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("airport")]&nbsp; &nbsp; &nbsp; &nbsp; public string Airport { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public partial class Origin&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("airport")]&nbsp; &nbsp; &nbsp; &nbsp; public string Airport { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("terminal")]&nbsp; &nbsp; &nbsp; &nbsp; public string Terminal { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public partial class Outbound&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("flights")]&nbsp; &nbsp; &nbsp; &nbsp; public List<OutboundFlight> Flights { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public partial class OutboundFlight&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("departs_at")]&nbsp; &nbsp; &nbsp; &nbsp; public string DepartsAt { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("arrives_at")]&nbsp; &nbsp; &nbsp; &nbsp; public string ArrivesAt { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("origin")]&nbsp; &nbsp; &nbsp; &nbsp; public Destination Origin { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("destination")]&nbsp; &nbsp; &nbsp; &nbsp; public Origin Destination { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("marketing_airline")]&nbsp; &nbsp; &nbsp; &nbsp; public string MarketingAirline { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("operating_airline")]&nbsp; &nbsp; &nbsp; &nbsp; public string OperatingAirline { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("flight_number")]&nbsp; &nbsp; &nbsp; &nbsp; public string FlightNumber { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("aircraft")]&nbsp; &nbsp; &nbsp; &nbsp; public string Aircraft { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("booking_info")]&nbsp; &nbsp; &nbsp; &nbsp; public BookingInfo BookingInfo { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public partial class Flight&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public static Flight FromJson(string json) => JsonConvert.DeserializeObject<Flight>(json, Flighting.Converter.Settings);&nbsp; &nbsp; }&nbsp; &nbsp; public static class Serialize&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public static string ToJson(this Flight self) => JsonConvert.SerializeObject(self, Flighting.Converter.Settings);&nbsp; &nbsp; }&nbsp; &nbsp; internal static class Converter&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MetadataPropertyHandling = MetadataPropertyHandling.Ignore,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DateParseHandling = DateParseHandling.None,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Converters = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }}然后在你的课堂上使用它var flight = Flighting.Flight.FromJson(yourjson);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string currency&nbsp; &nbsp;=&nbsp; &nbsp;flight.Currency;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var restrictions = flight.Results[0].Fare.Restrictions;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var totalprice = flight.Results[0].Fare.TotalPrice;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var adult = flight.Results[0].Fare.PricePerAdult;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var a = flight.Results[0].Itineraries[0].Inbound.Flights[0].Aircraft;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var bookingInfo = flight.Results[0].Itineraries[0].Inbound.Flights[0].BookingInfo;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var d = flight.Results[0].Itineraries[0].Inbound.Flights[0].DepartsAt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var f = flight.Results[0].Itineraries[0].Inbound.Flights[0].FlightNumber;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var op = flight.Results[0].Itineraries[0].Inbound.Flights[0].OperatingAirline;适当循环,我使用索引零作为测试用例
打开App,查看更多内容
随时随地看视频慕课网APP