如何处理军刀InstaFlight API JSON响应

时间:2017-04-10 15:13:12

标签: json vb.net sabre

我连接到saber InstaFlight API并获得了JSON结果。输出字符串太长,我可以得到它们的值。但我的方式是占用大量内存。我想要一种在VB.NET中占用更少内存和动态方式的方法。

以下代码工作正常,没有错误:

response2 = DirectCast(postReq.GetResponse(), HttpWebResponse)
reader2 = New StreamReader(response2.GetResponseStream())
postReq.ContentType = "application/json; charset=utf-8"
Dim ser1 As JObject = JObject.Parse(reader2.ReadToEnd())

我可以得到这样的值,没有错误:

ElapseTime1 = ser1("PricedItineraries")(0)("AirItinerary")("OriginDestinationOptions")("OriginDestinationOption")(0)("ElapsedTime").Value(Of String)()

但是,它需要数百个变量,这不是正确的方法。

输出JSON的示例:

  

{“PricedItineraries”:[{“AirItinerary”:{“OriginDestinationOptions”:{“OriginDestinationOption”:[{“FlightSegment”:[{“DepartureAirport”:{“LocationCode”:“JFK”},“ArrivalAirport”: {“LocationCode”:“LAS”},“MarketingAirline”:{“Code”:“AS”},“ArrivalTimeZone”:{“GMTOffset”: - 7},“TPA_Extensions”:{“eTicket”:{“Ind” :true}},“StopQuantity”:0,“ElapsedTime”:344,“ResBookDesigCode”:“R”,“MarriageGrp”:“O”,“Equipment”:{“AirEquipType”:320},“DepartureDateTime”:“ 2017-07-07T09:30:00“,”ArrivalDateTime“:”2017-07-07T12:14:00“,”FlightNumber“:1251,”OnTimePerformance“:{”Percentage“:70},”OperatingAirline“:{ “CompanyShortName”:“VIRGIN AMERICA”,“FlightNumber”:251,“Code”:“VX”},“DepartureTimeZone”:{“GMTOffset”: - 4}},{“DepartureAirport”:{“LocationCode”:“LAS “},”ArrivalAirport“:{”LocationCode“:”LAX“},”MarketingAirline“:{”Code“:”AS“},”ArrivalTimeZone“:{”GMTOffset“: - 7},”TPA_Extensions“:{”电子机票“:{”Ind“:true}},”StopQuantity“ :0,“ElapsedTime”:85,“ResBookDesigCode”:“R”,“MarriageGrp”:“O”,“Equipment”:{“AirEquipType”:320},“DepartureDateTime”:“2017-07-07T14:45: 00“,”ArrivalDateTime“:”2017-07-07T16:10:00“,”FlightNumber“:1475,”OnTimePerformance“:{”Percentage“:36},”OperatingAirline“:{”CompanyShortName“:”VIRGIN AMERICA“ ,“FlightNumber”:475,“Code”:“VX”},“DepartureTimeZone”:{“GMTOffset”: - 7}}],“ElapsedTime”:580},{“FlightSegment”:[{“DepartureAirport”:{ “LocationCode”:“LAX”},“ArrivalAirport”:{“LocationCode”:“LAS”},“MarketingAirline”:{“Code”:“AS”},“ArrivalTimeZone”:{“GMTOffset”: - 7}, “TPA_Extensions”:{“eTicket”:{“Ind”:true}},“StopQuantity”:0,“ElapsedTime”:71,“ResBookDesigCode”:“R”,“MarriageGrp”:“O”,“Equipment”: {“AirEquipType”:320},“DepartureDateTime”:“2017-07-08T17:00:00”,“ArrivalDateTime”:“2017-07-08T18:11:00”,“FlightNumber”:1480,“OnTimePerformance”: {“百分比”:55},“运营航空公司”:{“CompanyShortName”:“VIRGIN AMERICA”,“F lightNumber“:480,”Code“:”VX“},”DepartureTimeZone“:{”GMTOffset“: - 7}},{”DepartureAirport“:{”LocationCode“:”LAS“},”ArrivalAirport“:{”LocationCode “:”JFK“},”MarketingAirline“:{”Code“:”AS“},”ArrivalTimeZone“:{”GMTOffset“: - 4},”TPA_Extensions“:{”eTicket“:{”Ind“:true} },“StopQuantity”:0,

1 个答案:

答案 0 :(得分:1)

我认为你需要为这个结果创建一个模型。根据这个JSON,你必须创建一个类模型。

如果你有这个JSON:

{
"dailyDealId": "432",
"discountPercentage": "0",
"product": {
    "productId": "10",
    "brandId": "10",
    "departmentId": "3",
    "name": "Baby Girl Velour Tunic & Snowflake Legging Set",
    "description": "The pretty set",
    "url": "http://whatever.whatever.com/files/whatever.tif"
}

你需要这个模型:

public class Product
{
    public string productId { get; set; }
    public string brandId { get; set; }
    public string departmentId { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string url { get; set; }
}

public class Data
{
    public string dailyDealId { get; set; }
    public string discountPercentage { get; set; }
    public Product product { get; set; }
}