0
0
C Sharp (C#)programming~30 mins

Working with JSON files in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Working with JSON files
📖 Scenario: You are building a simple program to manage a list of books in a library. The data will be saved and loaded using JSON files.
🎯 Goal: Create a C# program that stores book information in a list, converts it to JSON format, and prints the JSON string.
📋 What You'll Learn
Create a list of books with specific properties
Add a configuration variable for JSON formatting
Serialize the list of books to a JSON string
Print the JSON string to the console
💡 Why This Matters
🌍 Real World
Many applications save and load data in JSON format because it is easy to read and write for both humans and computers.
💼 Career
Knowing how to work with JSON files is essential for software developers, especially when building web services, APIs, or data-driven applications.
Progress0 / 4 steps
1
Create the list of books
Create a list called books of type List<Book> and add these exact books: new Book { Title = "The Hobbit", Author = "J.R.R. Tolkien", Year = 1937 } and new Book { Title = "1984", Author = "George Orwell", Year = 1949 }. Also, define the Book class with public properties Title, Author, and Year.
C Sharp (C#)
Need a hint?

Define a class Book with three public properties. Then create a List<Book> and add two Book objects with the exact details.

2
Add JSON serialization options
Add a variable called options of type System.Text.Json.JsonSerializerOptions and set its WriteIndented property to true.
C Sharp (C#)
Need a hint?

Use new JsonSerializerOptions { WriteIndented = true } to create the options variable.

3
Serialize the books list to JSON
Create a string variable called jsonString and assign it the JSON serialization of books using JsonSerializer.Serialize with options.
C Sharp (C#)
Need a hint?

Use JsonSerializer.Serialize(books, options) to convert the list to a JSON string.

4
Print the JSON string
Write a line to print the jsonString variable to the console using Console.WriteLine.
C Sharp (C#)
Need a hint?

Use Console.WriteLine(jsonString); to display the JSON output.