我用这行来反序列化json:
YahooRootObject contacts = new JavaScriptSerializer().Deserialize<YahooRootObject>(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; }
}
反序列化之后我想使用foreach迭代器,为此我需要在 contacts 对象上实现枚举器。
我的问题是如何在 contacts 对象上实现枚举器以使用foreach迭代器?
答案 0 :(得分:1)
只需将IEnumerable<YahooContact>
实施添加到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();
}
}