I'm serializing a multi-level json object. These are called webinars and each contain information like below. I can access the first level with no problems. I can access the 2nd level (categories >) if I do so like webinars[webinar].categories.TimeZone etc but I can't loop through it. When I try to foreach(category in webinar) {} I get:
CS1579 C# foreach statement cannot operate on variables of type because does not contain a public definition for 'GetEnumerator'
What have I done wrong?
thanks for your help.
SAMPLE JSON
{
"date": "2017-11-08T02:33:57Z",
"title": "Craft, The",
"desc": "Galeazzi's fx l rad, subs for opn fx type 3A/B/C w malunion",
"startTime": "4:03 AM",
"endTime": "5:46 PM",
"categories": {
"category": "e-learning",
"timeZone": "Asia/Jakarta",
"language": "English"
}
}
Model
public class Rootobject
{
public Webinar[] Webinars { get; set; }
public ELearning[] Elearning { get; set; }
}
public class Webinar
{
public DateTime Date { get; set; }
public string Title { get; set; }
public string Desc { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public WebinarCategories Categories { get; set; }
}
public class WebinarCategories
{
public string Category { get; set; }
public string TimeZone { get; set; }
public string Language { get; set; }
}
What I'm trying to achieve
foreach (var webinar in root.webinars) {
foreach (var category in webinar) {
<button href="javascript:void(0)" class="dropbtn">@category</button>
}
}
Is pretty simple why this won't work.
If you want to loop through the categories you have to write
foreach (var category in webinar.Categories)
Then will you get an error because WebinarCategories
does not implement GetEnumerator
because it does not implement IEnumerable
. If you want to store a list of categories in your webinar the easiest way would be to change your property like this.
public List<WebinarCategories> Categories { get; set; }
Change public WebinarCategories Categories { get; set; }
public WebinarCategories Categories { get; set; }
to public List<WebinarCategories> Categories { get; set; }
public List<WebinarCategories> Categories { get; set; }
or, if you really want to IEnumerable<WebinarCategories> Categories { get; set; }
IEnumerable<WebinarCategories> Categories { get; set; }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.