【问题标题】:Iteration cannot operate on variables of type public definition for 'getenumerator'迭代不能对“getenumerator”的公共定义类型的变量进行操作
【发布时间】:2021-03-12 09:02:27
【问题描述】:

我正在搜索其中进行 API 调用并将 XML 响应以及 SerializeXmlNode 和 DeserializeObject 获取到我的根对象。现在的问题是当我尝试使用 foreach 循环时。

我在下面收到此错误:

foreach 语句无法对 (Model.AccountLite) 类型的变量进行操作,因为不包含“getenumerator”的公共实例定义

我已经检查了这个 data = JsonConvert.DeserializeObject(json); 并且我可以看到数据。

我已经尝试过查看这个previously asked question

搜索 API 调用

    public static List<AccountLite> searchAccounts(string searchString)
    {
        List<AccountLite> result = new List<AccountLite>();
        Root data = new Root();

        string[] contains = searchString.Split(' ');
        RestClient client = new RestClient(baseUrl);

        foreach (string contain in contains)
        {
            if (contain.Length < 3) continue;

            RestRequest request = new RestRequest($"/xx/xx/xx/xxx/xxx/account?xx=Lite&searchString={searchString}");
            String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
            request.AddHeader("Authorization", "Basic " + encoded);

            IRestResponse response = client.Execute(request);
            string requestResponse = response.Content;

            //Converting data from XML into Json and deserializet json object
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(requestResponse);
                string json = JsonConvert.SerializeXmlNode(doc);
                data = JsonConvert.DeserializeObject<Root>(json);
            }
            catch (Exception)
            {
                continue;
            }

            if (data?.SiebelMessageEnvelope?.ListOfAccountLite?.AccountLite == null)
                continue;

            //this line is the one showing error.
            foreach (AccountLite item in data.SiebelMessageEnvelope.ListOfAccountLite.AccountLite)
            {
                bool containsBoth = true;
                foreach (string contain2 in contains)
                {

                    if (!item.Name.ToLower().Contains(contain2.ToLower()) && !item.Id.ToLower().Contains(contain2.ToLower()))
                        containsBoth = false;
                }
                if (containsBoth)
                {
                    if (result.FirstOrDefault(i => i.Id == item.Id) == null)
                    {
                        result.Add(item);
                    }
                }
            }
        }

        return result;
    }

型号

    public class AccountLite
    {
        public string Id { get; set; }
        public string AccountStatus { get; set; }
        public string AccountTypeCode { get; set; }
        public string Location { get; set; }
        public string Name { get; set; }
        public string SRIntegrationFlag { get; set; }
    }

    public class ListOfAccountLite
    {
        public AccountLite AccountLite { get; set; }
    }

    public class SiebelMessageEnvelope
    {
        [JsonProperty("@xmlns")]
        public string Xmlns { get; set; }
        public ListOfAccountLite ListOfAccountLite { get; set; }
    }

    public class Root
    {
        public SiebelMessageEnvelope SiebelMessageEnvelope { get; set; }
    }

Json 对象

 {

 "SiebelMessageEnvelope":{
  "@xmlns":"",
  "ListOfAccountLite":{
     "AccountLite":{
        "Id":"",
        "AccountStatus":"",
        "AccountTypeCode":"",
        "Location":"",
        "Name":"",
        "SRIntegrationFlag":""
     }
    }
  }
}

【问题讨论】:

    标签: c# json serialization foreach deserialization


    【解决方案1】:

    您的ListOfAccountLite 只包含一个AccountLiteforeach 在单个对象上没有意义,该对象不可枚举(意思是:实现 IEnumerable[&lt;T&gt;] 或包含显式 GetEnumerator() 方法)。

    只有一个物件,所以……拿去吧。而不是

    if (data?.SiebelMessageEnvelope?.ListOfAccountLite?.AccountLite == null)
                    continue;
    
    foreach (AccountLite item in data.SiebelMessageEnvelope.ListOfAccountLite.AccountLite)
    {
        // ... do the thing
    }
    

    简单

    var item = data?.SiebelMessageEnvelope?.ListOfAccountLite?.AccountLite;
    if (item is null)
        continue;
    
    // ... do the thing
    

    也就是说:您可能应该调查 JSON 等中的 ListOfAccountLite 是否意味着是一个 数组 而不是单个对象。

    【讨论】:

    • 我应该用整个 foreach 替换吗? var item = data?.SiebelMessageEnvelope?.ListOfAccountLite?.AccountLite;如果 item 不为 null 则继续?
    • 您能否指导我将 ListOfAccountLite 更改为列表,以便我可以有两个选项,单个结果和结果列表
    • @ITForward 拥有可以要么 是列表 单个结果的东西坦率地说是一种糟糕的API 方法;唯一的“正确”修复是总是是一个可能包含零个、一个或多个元素的列表;在 JSON 术语中:"ListOfAccountLite":[{...}]
    • 我们能否更改列表的 ListOfAccountLite,这意味着它将返回一个帐户列表
    • @ITForward 是的,绝对... 如果您也相应地更改了 JSON;显示的 JSON 只能传达一个帐户
    猜你喜欢
    • 2012-08-19
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多