-1

I'm making an application in C# for my teacher to use for student attendance and because I'm just starting C# (came from Java), I need someone to guide me in the right direction.

I have Json file containing 2 arrays, one for morning students and one for afternoon students, and I need to parse them both into separate lists. In the Json file, they both have separate names ("morning" and "afternoon"). How can I accomplish this with Json.NET?

I have 2 lists already, morningStudents and afternoonStudents each containing students. The Student class contains 3 variables, name, id, and status which corresponds with the fields each student gets in each student array.

Below you can find a representation of what my Json file looks like.

{
    "morning": 
    [
        {
            "name": "Morning Student 1",
            "id": "123456",
            "status": "0"
        }
    ],
    "afternoon": 
    [
        {
            "name": "Afternoon Student 1",
            "id": "123456",
            "status": "0"
        }
    ]
}
Cooper
  • 11
  • 2
  • Do you have class definitions in C# for `Student` that you can add to the question? – Becca Dee Feb 01 '19 at 00:02
  • What have you tried? A object with two collections within the object would allow you to do JsonConvert.Deserialize<...>(...) but we need more information. – Greg Feb 01 '19 at 00:03
  • Please see [How to auto-generate a C# class file from a JSON object string](https://stackoverflow.com/q/21611674/3744182). – dbc Feb 01 '19 at 00:42

1 Answers1

1

I'd suggest you have two .NET classes, one representing a student and one representing a schedule. The StudentSchedule class would have a morning list-of-Student property and an evening list-of-Student property. You can then use JSON.NET to deserialize that directly.

using System;                   
using System.Collections.Generic;
using Newtonsoft.Json;

public class Student {
    public String name { get; set; }
    public String id { get; set; }
    public Int32 status { get; set; }
    public override String ToString() {
        return name + "(" + id + "): " + status.ToString();
    }
}

public class StudentSchedule {
    public IList<Student> morning { get; set; }
    public IList<Student> afternoon { get; set; }
}

public class Program
{
    public static void Main()
    {
        String myJson = @"{
    'morning': 
    [
        {
            'name': 'Morning Student 1',
            'id': '123456',
            'status': '0'
        }
    ],
    'afternoon': 
    [
        {
            'name': 'Afternoon Student 1',
            'id': '123456',
            'status': '0'
        }
    ]
}";
        StudentSchedule studentSchedule = JsonConvert.DeserializeObject<StudentSchedule>(myJson);
        Console.WriteLine("========== MORNING ===========");        
        foreach(Student student in studentSchedule.morning) {
            Console.WriteLine(student);
        }
        Console.WriteLine("========== AFTERNOON ===========");      
        foreach(Student student in studentSchedule.afternoon) {
            Console.WriteLine(student);
        }
    }
}

I put up a fiddle with this code for you to look at: https://dotnetfiddle.net/f7InH7.

You could deserialized to a dynamic object as well as in this question. Since you are new to C#, I would avoid dynamics for now and focus on getting the fundamentals right first.

See https://www.newtonsoft.com/json/help/html/SerializingJSON.htm for more documentation on JSON.NET.

Becca Dee
  • 1,530
  • 1
  • 24
  • 51