无法将类型的“ System.Collections.ArrayList”转换为MyWebServiceCall.Ser



我试图将数组列表如下所示,但会得到上述错误。该代码仅通过Myitem来正常工作,但我还需要通过多个。

这是我的代码:

var deserializedm = JsonConvert.DeserializeObject<List<Mulitipay>>(mpay.ToString());
int num = 0;
ArrayList list = new ArrayList();
foreach (Mulitipay m in deserializedm)
{
    num++;
    string f = m.Fund;
    string a = m.Amount;
    int am = System.Convert.ToInt32(a);
    string g = m.Gl;
    string d = m.Details;

    var itd = new ServiceReference1.glItemDetails();
    itd.fundCode = f;
    itd.additionalReference = reference;

    var its = new ServiceReference1.Data();
    its.description = d;
    its.reference = g;
    its.amountInMinorUnits = am;
    var myitem = new ServiceReference1.sampleGroupItem();
    myitem.lgItemDetails = itd;
    myitem.itemSummary = its;
    myitem.lineId = "tetetetet1232323";
    list.Add(myitem);
}
var alllist = list;
simpleivr.sale = new ServiceReference1.samplesale ();
simpleivr.sale.saleSummary = new ServiceReference1.Data();
simpleivr.sale.saleSummary.description = "stuff1";
simpleivr.sale.saleSummary.reference = "000303";
simpleivr.sale.saleSummary.amountInMinorUnits = 400;
simpleivr.sale.items = new ServiceReference1.SampleGroupItem [] { alllist};

WebService(Web服务的示例不完整)

/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Order=2)]
[System.Xml.Serialization.XmlArrayItemAttribute("Groupitem" IsNullable=false)]
public SampleGroupItem[] GroupItems{
    get {
        return this.itemsField;
    }
    set {
        this.itemsField = value;
        this.RaisePropertyChanged("Groupitems");
    }
}

数组列表与List<Object>相似。您将其分配给SampleGroupItem类型的数组。您只需要将ArrayList更改为List<SampleGroupItem>,然后调用方法.ToArray()
因此,您的代码将成为

  var deserializedm = JsonConvert.DeserializeObject<List<Mulitipay>>(mpay.ToString());
        int num = 0;
        List<ServiceReference1.SampleGroupItem> list = new List<ServiceReference1.SampleGroupItem>();
        foreach (Mulitipay m in deserializedm)
        {
            num++;
            string f = m.Fund;
            string a = m.Amount;
            int am = System.Convert.ToInt32(a);
            string g = m.Gl;
            string d = m.Details;

            var itd = new ServiceReference1.glItemDetails();
            itd.fundCode = f;
            itd.additionalReference = reference;

            var its = new ServiceReference1.Data();
            its.description = d;
            its.reference = g;
            its.amountInMinorUnits = am;
            var myitem = new ServiceReference1.sampleGroupItem();
            myitem.lgItemDetails = itd;
            myitem.itemSummary = its;
            myitem.lineId = "tetetetet1232323";
            list.Add(myitem);
        }
        simpleivr.sale = new ServiceReference1.samplesale ();
        simpleivr.sale.saleSummary = new ServiceReference1.Data();
        simpleivr.sale.saleSummary.description = "stuff1";
        simpleivr.sale.saleSummary.reference = "000303";
        simpleivr.sale.saleSummary.amountInMinorUnits = 400;
        simpleivr.sale.items = list.ToArray();

相关内容

  • 没有找到相关文章

最新更新