简体   繁体   English

将响应对象类型转换为列表C#

[英]cast a response object type to a list C#

i am hitting a webservice which is returning me a object Items in which i have a collection airpricepoint. 我遇到了一个Web服务,正在向我返回一个对象物品,其中我有一个airpricepoint集合。 I have created similar classes items and airpricepoint i am trying to cast the lowFareSearchRsp.Items to IList collection so that i can foreach loop and access the inner collections but i am unable to loop through i cant access the collection inside of fareitems 我创建了类似的类项目和airpricepoint,我试图将lowFareSearchRsp.Items转换为IList集合,以便我可以foreach循环并访问内部集合,但无法循环,无法访问fareitems内部的集合

IList collection = (IList)lowFareSearchRsp.Items;
if (collection != null)
{
    foreach (var fareitems in collection)
    {
        items itemsobj = new items();
        Document.airpricepoint airpricepointobj = new airpricepoint();
        airpricepointobj.AirPricingInfo = new List<airpricinginfo>();
        airpricepointobj.AirPricingResultMessage = fareitems.AirPricingResultMessage;
        airpricepointobj.FeeInfo = fareitems.FeeInfo;
        airpricepointobj.FareNote = fareitems.FareNote;
        airpricepointobj.TaxInfo = fareitems.TaxInfo;
        airpricepointobj.Key = fareitems.Key;
        airpricepointobj.TotalPrice = fareitems.TotalPrice;
        airpricepointobj.BasePrice = fareitems.BasePrice;
    }


public class items
{
    public IList<airpricepoint> AirPricePoint { get; set; }
}

public class airpricepoint
{
    public IList<airpricinginfo> AirPricingInfo { get; set; }
    public object AirPricingResultMessage { get; set; }
    public object FeeInfo { get; set; }
    public object FareNote { get; set; }
    public object TaxInfo { get; set; }
    public string Key { get; set; }
    public string TotalPrice { get; set; }
    public string BasePrice { get; set; }
}

If it's indeed a web service, then you already know the structure (format) of the data you're receiving. 如果确实是Web服务,那么您已经知道要接收的数据的结构(格式)。 Usually it will be in JSON or XML format; 通常它将采用JSON或XML格式; or packaged in a contract class if it's a WCF service. 或打包为WCF服务的合同类。

This means that you can simply de-serialize the data using any JSON or XML serialization library. 这意味着您可以简单地使用任何JSON或XML序列化库反序列化数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM