为了账号安全,请及时绑定邮箱和手机立即绑定

从嵌套的 Json 中检索值

从嵌套的 Json 中检索值

C#
明月笑刀无情 2021-07-01 10:01:14
我有一个 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”中的值。
查看完整描述

2 回答

?
慕丝7291255

TA贡献1859条经验 获得超6个赞

从此链接将此 C# 模型用于您的 json


namespace Flighting

{

    using System;

    using System.Collections.Generic;


    using System.Globalization;

    using Newtonsoft.Json;

    using Newtonsoft.Json.Converters;


    public partial class Flight

    {

        [JsonProperty("currency")]

        public string Currency { get; set; }


        [JsonProperty("results")]

        public List<Result> Results { get; set; }

    }


    public partial class Result

    {

        [JsonProperty("itineraries")]

        public List<Itinerary> Itineraries { get; set; }


        [JsonProperty("fare")]

        public Fare Fare { get; set; }

    }


    public partial class Fare

    {

        [JsonProperty("total_price")]

        public string TotalPrice { get; set; }


        [JsonProperty("price_per_adult")]

        public PricePerAdult PricePerAdult { get; set; }


        [JsonProperty("restrictions")]

        public Restrictions Restrictions { get; set; }

    }


    public partial class PricePerAdult

    {

        [JsonProperty("total_fare")]

        public string TotalFare { get; set; }


        [JsonProperty("tax")]

        public string Tax { get; set; }

    }


    public partial class Restrictions

    {

        [JsonProperty("refundable")]

        public bool Refundable { get; set; }


        [JsonProperty("change_penalties")]

        public bool ChangePenalties { get; set; }

    }


    public partial class Itinerary

    {

        [JsonProperty("outbound")]

        public Outbound Outbound { get; set; }


        [JsonProperty("inbound")]

        public Inbound Inbound { get; set; }

    }


    public partial class Inbound

    {

        [JsonProperty("flights")]

        public List<InboundFlight> Flights { get; set; }

    }


    public partial class InboundFlight

    {

        [JsonProperty("departs_at")]

        public string DepartsAt { get; set; }


        [JsonProperty("arrives_at")]

        public string ArrivesAt { get; set; }


        [JsonProperty("origin")]

        public Origin Origin { get; set; }


        [JsonProperty("destination")]

        public Destination Destination { get; set; }


        [JsonProperty("marketing_airline")]

        public string MarketingAirline { get; set; }


        [JsonProperty("operating_airline")]

        public string OperatingAirline { get; set; }


        [JsonProperty("flight_number")]

        public string FlightNumber { get; set; }


        [JsonProperty("aircraft")]

        public string Aircraft { get; set; }


        [JsonProperty("booking_info")]

        public BookingInfo BookingInfo { get; set; }

    }


    public partial class BookingInfo

    {

        [JsonProperty("travel_class")]

        public string TravelClass { get; set; }


        [JsonProperty("booking_code")]

        public string BookingCode { get; set; }


        [JsonProperty("seats_remaining")]

        public long SeatsRemaining { get; set; }

    }


    public partial class Destination

    {

        [JsonProperty("airport")]

        public string Airport { get; set; }

    }


    public partial class Origin

    {

        [JsonProperty("airport")]

        public string Airport { get; set; }


        [JsonProperty("terminal")]

        public string Terminal { get; set; }

    }


    public partial class Outbound

    {

        [JsonProperty("flights")]

        public List<OutboundFlight> Flights { get; set; }

    }


    public partial class OutboundFlight

    {

        [JsonProperty("departs_at")]

        public string DepartsAt { get; set; }


        [JsonProperty("arrives_at")]

        public string ArrivesAt { get; set; }


        [JsonProperty("origin")]

        public Destination Origin { get; set; }


        [JsonProperty("destination")]

        public Origin Destination { get; set; }


        [JsonProperty("marketing_airline")]

        public string MarketingAirline { get; set; }


        [JsonProperty("operating_airline")]

        public string OperatingAirline { get; set; }


        [JsonProperty("flight_number")]

        public string FlightNumber { get; set; }


        [JsonProperty("aircraft")]

        public string Aircraft { get; set; }


        [JsonProperty("booking_info")]

        public BookingInfo BookingInfo { get; set; }

    }


    public partial class Flight

    {

        public static Flight FromJson(string json) => JsonConvert.DeserializeObject<Flight>(json, Flighting.Converter.Settings);

    }


    public static class Serialize

    {

        public static string ToJson(this Flight self) => JsonConvert.SerializeObject(self, Flighting.Converter.Settings);

    }


    internal static class Converter

    {

        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings

        {

            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,

            DateParseHandling = DateParseHandling.None,

            Converters = {

                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }

            },

        };

    }

}

然后在你的课堂上使用它


var flight = Flighting.Flight.FromJson(yourjson);


         string currency   =   flight.Currency;

            var restrictions = flight.Results[0].Fare.Restrictions;

            var totalprice = flight.Results[0].Fare.TotalPrice;

            var adult = flight.Results[0].Fare.PricePerAdult;

            var a = flight.Results[0].Itineraries[0].Inbound.Flights[0].Aircraft;

            var bookingInfo = flight.Results[0].Itineraries[0].Inbound.Flights[0].BookingInfo;

            var d = flight.Results[0].Itineraries[0].Inbound.Flights[0].DepartsAt;

            var f = flight.Results[0].Itineraries[0].Inbound.Flights[0].FlightNumber;

            var op = flight.Results[0].Itineraries[0].Inbound.Flights[0].OperatingAirline;

适当循环,我使用索引零作为测试用例


查看完整回答
反对 回复 2021-07-18
  • 2 回答
  • 0 关注
  • 170 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信