json被反序列化为C#对象后,如何使foreach迭代器工作?

json被反序列化为C#对象后,如何使foreach迭代器工作?,c#,.net,C#,.net,我使用此行反序列化json: YahooRootObject contacts = new JavaScriptSerializer().Deserialize<YahooRootObject>(stringToParse); YahooRootObject contacts=new JavaScriptSerializer()。反序列化(stringToParse); 以下是YahooRootObject: public class YahooRootObject {

我使用此行反序列化json:

YahooRootObject contacts = new JavaScriptSerializer().Deserialize<YahooRootObject>(stringToParse);
YahooRootObject contacts=new JavaScriptSerializer()。反序列化(stringToParse);
以下是YahooRootObject:

public class YahooRootObject
{
    public YahooContacts contacts { get; set; }
}

internal class YahooField
{
    public int id { get; set; }
    public string type { get; set; }
    public object value { get; set; }
    public string editedBy { get; set; }
    public List<object> flags { get; set; }
    public List<object> categories { get; set; }
    public string updated { get; set; }
    public string created { get; set; }
    public string uri { get; set; }
}

internal class YahooContact
{
    public bool isConnection { get; set; }
    public int id { get; set; }
    public List<YahooField> fields { get; set; }
    public List<object> categories { get; set; }
    public int error { get; set; }
    public int restoredId { get; set; }
    public string created { get; set; }
    public string updated { get; set; }
    public string uri { get; set; }
}

internal class YahooContacts
{
    public List<YahooContact> contact { get; set; }
    public int count { get; set; }
    public int start { get; set; }
    public int total { get; set; }
    public string uri { get; set; }
    public bool cache { get; set; }
}
公共类YahooRootObject
{
公共联系人联系人{get;set;}
}
内部类YahooField
{
公共int id{get;set;}
公共字符串类型{get;set;}
公共对象值{get;set;}
由{get;set;}编辑的公共字符串
公共列表标志{get;set;}
公共列表类别{get;set;}
公共字符串已更新{get;set;}
已创建公共字符串{get;set;}
公共字符串uri{get;set;}
}
内部类Yahoo触点
{
公共布尔值断开连接{get;set;}
公共int id{get;set;}
公共列表字段{get;set;}
公共列表类别{get;set;}
公共整数错误{get;set;}
公共int restoredId{get;set;}
已创建公共字符串{get;set;}
公共字符串已更新{get;set;}
公共字符串uri{get;set;}
}
内部类Yahoo联系人
{
公共列表联系人{get;set;}
公共整数计数{get;set;}
public int start{get;set;}
公共整数总计{get;set;}
公共字符串uri{get;set;}
公共布尔缓存{get;set;}
}
在反序列化之后,我想使用foreach迭代器,为此,我需要在contacts对象上实现枚举器


我的问题是如何在联系人对象上实现枚举器以使用foreach迭代器

只需将
IEnumerable
实现添加到
YahooContacts
类:

internal class YahooContacts : IEnumerable<YahooContact>
{
    public List<YahooContact> contact { get; set; }
    public int count { get; set; }
    public int start { get; set; }
    public int total { get; set; }
    public string uri { get; set; }
    public bool cache { get; set; }

    public IEnumerator<YahooContact> GetEnumerator()
    {
        return contact.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return contact.GetEnumerator();
    }
}
内部类:IEnumerable
{
公共列表联系人{get;set;}
公共整数计数{get;set;}
public int start{get;set;}
公共整数总计{get;set;}
公共字符串uri{get;set;}
公共布尔缓存{get;set;}
公共IEnumerator GetEnumerator()
{
返回contact.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
返回contact.GetEnumerator();
}
}

JSON与您的问题有什么关系?为什么不能在已经存在的列表上使用foreach?
foreach(contacts.contact中的var c)
?此时您可以编写
foreach(root.contacts.contacts中的var c)
。你希望能写些什么?如果根对象只包含一个
YahooContacts
对象,那么它在API中是否仍然是必需的?我不能这样做:foreach(contacts.contact中的var c)我收到了这个错误:“MvcContactsImporter.Models.DataTypes.Yahoo.API.YahooContacts”不包含“GetEnumerator”的公共定义