Skip to content

Instantly share code, notes, and snippets.

@MirzaLeka
Last active July 28, 2024 08:03
Show Gist options
  • Save MirzaLeka/ec4ae3fb5985706fc33d6a2f99621bd7 to your computer and use it in GitHub Desktop.
Save MirzaLeka/ec4ae3fb5985706fc33d6a2f99621bd7 to your computer and use it in GitHub Desktop.
Read JSON file into a C# Classes

Read JSON file into a C# Classes

Create the class skeleton

using Newtonsoft.Json; // Requires installation

public class LINQTutorial
{
    private List<Student> _students;

    // you may not need this
    public static void Main()
    {
        
    }
    
    public LINQTutorial()
    {
        PopulateStudentsData();
    }

    public List<Student> GetStudents()
    {
        return _students;
    }

    void PopulateStudentsData()
    {
        /* TODO */
    }
    
}

Then read file (students.json) content and converting into C# model.

    void PopulateStudentsData()
    {
        // Get the directory (mine is in \RiderProjects\LearnCSharp\bin\Debug\net8.0\students.json)
        string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;

        // Join with the file relative path
        string filePath = Path.Combine(currentDirectory, "students.json");

        // Read JSON file
        string json = File.ReadAllText(filePath);

        // Deserialize to list of Student objects and assign to _students
        _students = JsonConvert.DeserializeObject<List<Student>>(json);
    }

Adding Newtensoft

To parse (Deserialize) JSON to C# you need to install and import Newtonsoft.Json package.

image

a) You can do this either by pressing CTRL + . when you're over the JsonConvert.

image

b) Or opening the Nuget Package manager, searching for Newtonsoft dependency and installing it in the project image

Play Test

To test it, create an instance of the LINQTutorial class and call the GetStudents() method.

var LT = new LINQTutorial();
var students = LT.GetStudents();

For demonstration purposes I'll run a quick LINQ:

var LT = new LINQTutorial();
var students = LT.GetStudents();

var firstStudent = students.First();

Console.WriteLine($"Name: {firstStudent.Name}, Age: {firstStudent.Age}, Country: {firstStudent.Country}");
// Name: Mirza, Age: 18, Country: Bosnia

And that's it!

/// <summary>
/// Base Class for Classes
/// </summary>
public class Classes
{
/// <summary>
/// Non-Unique ID
/// </summary>
public int ID { get; set; }
/// <summary>
/// Class TItle
/// </summary>
public string Title { get; set; }
}
/// <summary>
/// Base Class for Student
/// </summary>
public class Student
{
/// <summary>
/// Student Unique ID
/// </summary>
public int ID { get; set; }
/// <summary>
/// Student Name
/// </summary>
public string Name { get; set; }
/// <summary>
/// Student Age
/// </summary>
public int Age { get; set; }
/// <summary>
/// From Country
/// </summary>
public string Country { get; set; }
/// <summary>
/// Subjects / Classes taken
/// </summary>
public List<Classes> Classes { get; set; }
}
[
{
"ID": 1,
"Name": "Mirza",
"Age": 18,
"Country": "Bosnia",
"Classes": [
{
"ID": 1,
"Title": "CAD"
},
{
"ID": 2,
"Title": "IT"
}
]
},
{
"ID": 2,
"Name": "Armin",
"Age": 20,
"Country": "Bosnia",
"Classes": [
{
"ID": 1,
"Title": "Math"
},
{
"ID": 2,
"Title": "IT"
},
{
"ID": 3,
"Title": "History"
}
]
},
{
"ID": 3,
"Name": "Alan",
"Age": 19,
"Country": "UK",
"Classes": [
{
"ID": 1,
"Title": "Business Analysis"
}
]
},
{
"ID": 4,
"Name": "Seid",
"Age": 22,
"Country": "Egypt",
"Classes": [
{
"ID": 1,
"Title": "Business Analysis"
},
{
"ID": 2,
"Title": "English"
}
]
},
{
"ID": 5,
"Name": "Farook",
"Age": 18,
"Country": "Turkey",
"Classes": [
{
"ID": 1,
"Title": "IT"
}
]
},
{
"ID": 6,
"Name": "Raj",
"Age": 20,
"Country": "India",
"Classes": [
{
"ID": 1,
"Title": "Quality Assurance"
}
]
},
{
"ID": 7,
"Name": "Nihad",
"Age": 24,
"Country": "Turkey",
"Classes": [
{
"ID": 1,
"Title": "Economy"
},
{
"ID": 2,
"Title": "Biology"
}
]
},
{
"ID": 8,
"Name": "Eddy",
"Age": 18,
"Country": "China",
"Classes": [
{
"ID": 1,
"Title": "Quality Assurance"
},
{
"ID": 2,
"Title": "Math"
}
]
},
{
"ID": 9,
"Name": "Abdurahman",
"Age": 19,
"Country": "Egypt",
"Classes": [
{
"ID": 1,
"Title": "Economy"
},
{
"ID": 2,
"Title": "Math"
}
]
},
{
"ID": 10,
"Name": "Amy",
"Age": 21,
"Country": "USA",
"Classes": [
{
"ID": 1,
"Title": "IT"
},
{
"ID": 2,
"Title": "English"
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment