首页 > 解决方案 > 如何从json中的不同类中获取数据值?例如使用其 id 的名称

问题描述

我目前正在用 c# 制作一个航班搜索应用程序。我将 json 绑定到 datagridview 中。到目前为止,这是我的代码。

 var client = new RestClient("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/GB/GBP/en-GB/UK-sky/JFK-sky/2021-09-01?inboundpartialdate=2021-09-10");
            var request = new RestRequest(Method.GET);
            request.AddHeader("x-rapidapi-key", "d6deba1f4bmsh21094fa230617cdp180268jsn452ac752d55a");
            request.AddHeader("x-rapidapi-host", "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com");
            IRestResponse response = client.Execute(request);
            dynamic results = JsonConvert.DeserializeObject<Application>(response.Content);

            DataTable table = new DataTable();


            string quoteID;
            string strPrice;
            string Destination;
            string Origin;
            string Carrier;
            string Dates;
            foreach (var a in results.Quotes)
            {
                
                quoteID = String.Format("{0}", a.QuoteId);
                Debug.WriteLine(quoteID);
                strPrice = String.Format("{0:C}", a.MinPrice);
                Debug.WriteLine(strPrice);
                Destination = String.Format("{0}", a.OutboundLeg.DestinationId);
                Debug.WriteLine("" + Destination);
                Origin = String.Format("{0}", a.OutboundLeg.OriginId);
                Debug.WriteLine("" + Origin);
                Carrier = String.Format("{0}", a.OutboundLeg.CarrierIds[0]);
                Debug.WriteLine("" + Carrier);
                DateTime Date = a.OutboundLeg.DepartureDate;
                Dates = Date.ToString("MM/dd/yyyy");
                Debug.WriteLine("" + Dates);
                dataGridView1.Rows.Add(
                                 quoteID,
                                 strPrice,
                                 Carrier,
                                 Destination,
                                 Origin,
                                 Dates);



            }

这是我的json。

{
  "Quotes" : [ {
    "QuoteId" : 1,
    "MinPrice" : 185,
    "Direct" : false,
    "OutboundLeg" : {
      "CarrierIds" : [ 1247 ],
      "OriginId" : 65655,
      "DestinationId" : 60987,
      "DepartureDate" : "2021-09-01T00:00:00"
    },
    "QuoteDateTime" : "2021-03-17T09:31:00"
  }, {
    "QuoteId" : 2,
    "MinPrice" : 310,
    "Direct" : false,
    "OutboundLeg" : {
      "CarrierIds" : [ 50441 ],
      "OriginId" : 43139,
      "DestinationId" : 60987,
      "DepartureDate" : "2021-09-01T00:00:00"
    },
    "QuoteDateTime" : "2021-03-16T20:39:00"
  }, {
    "QuoteId" : 3,
    "MinPrice" : 561,
    "Direct" : false,
    "OutboundLeg" : {
      "CarrierIds" : [ 1033 ],
      "OriginId" : 54353,
      "DestinationId" : 60987,
      "DepartureDate" : "2021-09-01T00:00:00"
    },
    "QuoteDateTime" : "2021-03-16T15:34:00"
  }, {
    "QuoteId" : 4,
    "MinPrice" : 806,
    "Direct" : false,
    "OutboundLeg" : {
      "CarrierIds" : [ 1033 ],
      "OriginId" : 67662,
      "DestinationId" : 60987,
      "DepartureDate" : "2021-09-01T00:00:00"
    },
    "QuoteDateTime" : "2021-03-16T21:47:00"
  }, {
    "QuoteId" : 5,
    "MinPrice" : 883,
    "Direct" : true,
    "OutboundLeg" : {
      "CarrierIds" : [ 1618 ],
      "OriginId" : 65698,
      "DestinationId" : 60987,
      "DepartureDate" : "2021-09-01T00:00:00"
    },
    "QuoteDateTime" : "2021-03-16T15:15:00"
  }, {
    "QuoteId" : 6,
    "MinPrice" : 2137,
    "Direct" : true,
    "OutboundLeg" : {
      "CarrierIds" : [ 1859 ],
      "OriginId" : 67662,
      "DestinationId" : 60987,
      "DepartureDate" : "2021-09-01T00:00:00"
    },
    "QuoteDateTime" : "2021-03-16T21:47:00"
  } ],
  "Carriers" : [ {
    "CarrierId" : 1033,
    "Name" : "Aer Lingus"
  }, {
    "CarrierId" : 1859,
    "Name" : "Virgin Atlantic"
  }, {
    "CarrierId" : 1618,
    "Name" : "Qatar Airways"
  }, {
    "CarrierId" : 50441,
    "Name" : "easyJet"
  }, {
    "CarrierId" : 1247,
    "Name" : "LEVEL"
  } ],
  "Places" : [ {
    "Name" : "Aberdeen",
    "Type" : "Station",
    "PlaceId" : 40074,
    "IataCode" : "ABZ",
    "SkyscannerCode" : "ABZ",
    "CityName" : "Aberdeen",
    "CityId" : "ABER",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Belfast City",
    "Type" : "Station",
    "PlaceId" : 42644,
    "IataCode" : "BHD",
    "SkyscannerCode" : "BHD",
    "CityName" : "Belfast",
    "CityId" : "BELF",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Birmingham",
    "Type" : "Station",
    "PlaceId" : 42664,
    "IataCode" : "BHX",
    "SkyscannerCode" : "BHX",
    "CityName" : "Birmingham",
    "CityId" : "BIRM",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Bristol",
    "Type" : "Station",
    "PlaceId" : 43139,
    "IataCode" : "BRS",
    "SkyscannerCode" : "BRS",
    "CityName" : "Bristol",
    "CityId" : "BRIS",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Cardiff",
    "Type" : "Station",
    "PlaceId" : 45676,
    "IataCode" : "CWL",
    "SkyscannerCode" : "CWL",
    "CityName" : "Cardiff",
    "CityId" : "CARD",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Edinburgh",
    "Type" : "Station",
    "PlaceId" : 49369,
    "IataCode" : "EDI",
    "SkyscannerCode" : "EDI",
    "CityName" : "Edinburgh",
    "CityId" : "EDIN",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Glasgow International",
    "Type" : "Station",
    "PlaceId" : 54353,
    "IataCode" : "GLA",
    "SkyscannerCode" : "GLA",
    "CityName" : "Glasgow",
    "CityId" : "GLAS",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Humberside",
    "Type" : "Station",
    "PlaceId" : 57113,
    "IataCode" : "HUY",
    "SkyscannerCode" : "HUY",
    "CityName" : "Kirmington",
    "CityId" : "HUMB",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Inverness",
    "Type" : "Station",
    "PlaceId" : 59078,
    "IataCode" : "INV",
    "SkyscannerCode" : "INV",
    "CityName" : "Inverness",
    "CityId" : "INVE",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Ronaldsway",
    "Type" : "Station",
    "PlaceId" : 59117,
    "IataCode" : "IOM",
    "SkyscannerCode" : "IOM",
    "CityName" : "Castletown",
    "CityId" : "CAST",
    "CountryName" : "Isle of Man"
  }, {
    "Name" : "Jersey",
    "Type" : "Station",
    "PlaceId" : 60946,
    "IataCode" : "JER",
    "SkyscannerCode" : "JER",
    "CityName" : "Jersey",
    "CityId" : "JERS",
    "CountryName" : "Jersey"
  }, {
    "Name" : "New York John F. Kennedy",
    "Type" : "Station",
    "PlaceId" : 60987,
    "IataCode" : "JFK",
    "SkyscannerCode" : "JFK",
    "CityName" : "New York",
    "CityId" : "NYCA",
    "CountryName" : "United States"
  }, {
    "Name" : "Leeds Bradford",
    "Type" : "Station",
    "PlaceId" : 65393,
    "IataCode" : "LBA",
    "SkyscannerCode" : "LBA",
    "CityName" : "Leeds",
    "CityId" : "LEED",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "London City",
    "Type" : "Station",
    "PlaceId" : 65465,
    "IataCode" : "LCY",
    "SkyscannerCode" : "LCY",
    "CityName" : "London",
    "CityId" : "LOND",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "London Gatwick",
    "Type" : "Station",
    "PlaceId" : 65655,
    "IataCode" : "LGW",
    "SkyscannerCode" : "LGW",
    "CityName" : "London",
    "CityId" : "LOND",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "London Heathrow",
    "Type" : "Station",
    "PlaceId" : 65698,
    "IataCode" : "LHR",
    "SkyscannerCode" : "LHR",
    "CityName" : "London",
    "CityId" : "LOND",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "London Luton",
    "Type" : "Station",
    "PlaceId" : 66270,
    "IataCode" : "LTN",
    "SkyscannerCode" : "LTN",
    "CityName" : "London",
    "CityId" : "LOND",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Manchester",
    "Type" : "Station",
    "PlaceId" : 67662,
    "IataCode" : "MAN",
    "SkyscannerCode" : "MAN",
    "CityName" : "Manchester",
    "CityId" : "MANC",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Teesside International",
    "Type" : "Station",
    "PlaceId" : 68229,
    "IataCode" : "MME",
    "SkyscannerCode" : "MME",
    "CityName" : "Durham",
    "CityId" : "MMEA",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Newcastle",
    "Type" : "Station",
    "PlaceId" : 70060,
    "IataCode" : "NCL",
    "SkyscannerCode" : "NCL",
    "CityName" : "Newcastle",
    "CityId" : "NEWC",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Newquay",
    "Type" : "Station",
    "PlaceId" : 70745,
    "IataCode" : "NQY",
    "SkyscannerCode" : "NQY",
    "CityName" : "Newquay",
    "CityId" : "NEWQ",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Norwich",
    "Type" : "Station",
    "PlaceId" : 71017,
    "IataCode" : "NWI",
    "SkyscannerCode" : "NWI",
    "CityName" : "Norwich",
    "CityId" : "NORW",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "Southampton",
    "Type" : "Station",
    "PlaceId" : 82165,
    "IataCode" : "SOU",
    "SkyscannerCode" : "SOU",
    "CityName" : "Southampton",
    "CityId" : "SOUT",
    "CountryName" : "United Kingdom"
  }, {
    "Name" : "London Stansted",
    "Type" : "Station",
    "PlaceId" : 82398,
    "IataCode" : "STN",
    "SkyscannerCode" : "STN",
    "CityName" : "London",
    "CityId" : "LOND",
    "CountryName" : "United Kingdom"
  } ],
  "Currencies" : [ {
    "Code" : "USD",
    "Symbol" : "$",
    "ThousandsSeparator" : ",",
    "DecimalSeparator" : ".",
    "SymbolOnLeft" : true,
    "SpaceBetweenAmountAndSymbol" : false,
    "RoundingCoefficient" : 0,
    "DecimalDigits" : 2
  } ]
}

我遇到的问题是我正在尝试使用 outboundleg 类中提供给我的 ID 来获取承运人和城市的名称。例如,如果我有 ID 1247,我想获取 Carriers 类中的值“LEVEL”。为了达到其他类中的值,我需要什么样的代码。就您在我的代码中看到的而言,我可以获得每个报价的 id 编号,我的目标是打印出 datagridview 中的名称而不是数字。

编辑代码

  foreach (var quote in quotesResponse.Quotes)
            {

                var carrierInfo = carriersResponse.Carriers
                                             .Where(x => quote.OutboundLeg.CarrierIds.Contains(x.CarrierId))
                                             .Select(x => x.Name);

                var destinationInfo = destinationResponse.Places.FirstOrDefault(item => quote.OutboundLeg.DestinationIds == item.PlaceId);
                strPrice = String.Format("{0:C}", quote.MinPrice);
                Debug.WriteLine(strPrice);
                DateTime Date = quote.OutboundLeg.DepartureDate;
                Dates = Date.ToString("MM/dd/yyyy");
                Debug.WriteLine("" + Dates);
                dataGridView1.Rows.Add(
                                 quote.QuoteId,
                                 strPrice,
                                 string.Join(", ", carrierInfo),
                                 destinationInfo.Name,
                                 quote.OutboundLeg.OriginId,
                                 Dates);

            }

标签: c#jsonapiclass

解决方案


推荐阅读