I am not an expert in C# nor .NET, but I have managed to get looping array data (courses) in external json to work. Now I am stuck in loop some array inside each course (the classes in the course).
Right now I get error "The JSON value could not be converted to MrCourses.ClassesList." in this:
var courses = JsonSerializer.Deserialize<List<Course>>(json);
But if I remove the line:
public ClassesList Classes { get; set; }
in MrCourses.cs the error goes away, but then I get error "'Course' does not contain a definition for 'Classes'..." in view.cshtml:
ClassesList classes = course.Classes;
Below is my code.
MrCourses.cs:
using System.Collections.Generic;
namespace MrCourses
{
public class CoursesList
{
public List<CoursesList> Courses { get; set; }
}
public class ClassesList
{
public List<CourseClass> MyClasses { get; set; }
}
public class Course
{
public int Id { get; set; }
public ClassesList Classes { get; set; }
}
public class CourseClass
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
}
Hi
I’m on my mobile here but I think I’ve spotted something to help. In you if check you are look at ClassesList.Count, I think this should be classes.Count instead.
public class Course
{
public int Id { get; set; }
public string Name { get; set; }
public string Headline { get; set; }
public string Abstract { get; set; }
public List<CourseClass> Classes { get; set; } // Needed to add this line
}
My view is then:
foreach (var item in course.Classes)
{
@item.Name
}
How to loop "inner" array in json
Hi forum,
I am not an expert in C# nor .NET, but I have managed to get looping array data (courses) in external json to work. Now I am stuck in loop some array inside each course (the classes in the course).
Right now I get error "The JSON value could not be converted to MrCourses.ClassesList." in this:
But if I remove the line:
in MrCourses.cs the error goes away, but then I get error "'Course' does not contain a definition for 'Classes'..." in view.cshtml:
Below is my code.
MrCourses.cs:
view.cshtml:
json.json:
Hi I’m on my mobile here but I think I’ve spotted something to help. In you if check you are look at ClassesList.Count, I think this should be classes.Count instead.
Paul
You should not have that many nested "list" classes.
Do something like this:
var courses = JsonSerializer.Deserialize<List<Course>>(json);
using System.Collections.Generic;
namespace MrCourses { public class Course { public int Id { get; set; } public List
}
Disclaimer: the code is written on a mobile, sorry for formatting.
Thanks, Malthe Petersen! That did the trick. 🙂
Only two classes and "public List
My view is then:
is working on a reply...