Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Martin Rud 261 posts 1002 karma points c-trib
    Apr 18, 2022 @ 09:21
    Martin Rud
    0

    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:

    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; }
        }
    }
    

    view.cshtml:

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage;
    @using System.Net;
    @using System.Collections.Generic;
    @using System.Text.Json;
    @using MrCourses;
    
    @{
        string json = "...";
        var courses = JsonSerializer.Deserialize<List<Course>>(json);
        if(courses.Count != 0) 
        {
            <div>
                @foreach (Course course in courses)
                {
                    ClassesList classes = course.Classes;
                    <div>
                        <h3>@course.Headline</h3>
                        <p>@course.Abstract</p>
                        @if(ClassesList.Count != 0)
                        {
                            // Loop ClassesList...
                        }
    
                    </div>
                }
            </div>
        }
    }
    

    json.json:

    [
      {
        "Name": "Course #1",
        "Classes": [
          {
            "Name": "Course #1, class #1",
            "Address": "Course #1, class #1 address"
          },
          {
            "Name": "Course #1, class #1",
            "Address": "Course #1, class #2 address"
          },
          {
            "Name": "Course #1, class #1",
            "Address": "Course #1, class #3 address"
          }
        ]
      },
      {
        "Name": "Course #2",
        "Classes": [
          {
            "Name": "Course #2, class #1",
            "Address": "Course #1, class #1 address"
          },
          {
            "Name": "Course #2, class #1",
            "Address": "Course #1, class #2 address"
          },
          {
            "Name": "Course #2, class #1",
            "Address": "Course #1, class #3 address"
          }
        ]
      }
    ]
    
  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    Apr 18, 2022 @ 09:56
    Paul Seal
    0

    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

  • Malthe Petersen 68 posts 383 karma points c-trib
    Apr 18, 2022 @ 11:09
    Malthe Petersen
    100

    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

    public class CourseClass
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
    }
    

    }

    Disclaimer: the code is written on a mobile, sorry for formatting.

  • Martin Rud 261 posts 1002 karma points c-trib
    Apr 18, 2022 @ 11:54
    Martin Rud
    0

    Thanks, Malthe Petersen! That did the trick. 🙂

    Only two classes and "public List

    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
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft