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

Working with JSON files in C Sharp (C#)

Choose your learning style9 modes available
Introduction

JSON files let programs save and share data in a simple text format. Working with JSON files helps your program read and write data easily.

Saving user settings so they stay the same next time you open the app.
Reading data from a web service that sends information in JSON format.
Storing a list of items like a shopping list or contacts.
Sharing data between different programs or devices.
Loading configuration details when your program starts.
Syntax
C Sharp (C#)
using System.Text.Json;
using System.IO;

// To read JSON from a file
string jsonString = File.ReadAllText("file.json");
var data = JsonSerializer.Deserialize<YourType>(jsonString);

// To write JSON to a file
string output = JsonSerializer.Serialize(data);
File.WriteAllText("file.json", output);

Use JsonSerializer from System.Text.Json namespace for easy JSON handling.

Replace YourType with the class that matches your JSON structure.

Examples
This reads a JSON file into a dictionary where keys and values are strings.
C Sharp (C#)
using System.Text.Json;
using System.IO;

// Reading JSON into a dictionary
string jsonString = File.ReadAllText("data.json");
var dict = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonString);
This saves a simple object with name and age to a JSON file.
C Sharp (C#)
using System.Text.Json;
using System.IO;

// Writing an object to JSON file
var person = new { Name = "Anna", Age = 28 };
string json = JsonSerializer.Serialize(person);
File.WriteAllText("person.json", json);
Sample Program

This program saves a user object to a JSON file and then reads it back to show the data.

C Sharp (C#)
using System;
using System.IO;
using System.Text.Json;

class Program
{
    public class User
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    static void Main()
    {
        // Create a user object
        var user = new User { Name = "John", Age = 30 };

        // Convert user to JSON string
        string jsonString = JsonSerializer.Serialize(user);

        // Save JSON string to file
        File.WriteAllText("user.json", jsonString);

        // Read JSON string back from file
        string readJson = File.ReadAllText("user.json");

        // Convert JSON string back to User object
        var userFromFile = JsonSerializer.Deserialize<User>(readJson);

        // Show user info
        Console.WriteLine($"Name: {userFromFile.Name}, Age: {userFromFile.Age}");
    }
}
OutputSuccess
Important Notes

Make sure your classes have public properties with getters and setters for JSON serialization.

If the JSON file is missing or corrupted, reading it will cause an error, so handle exceptions in real apps.

Summary

JSON files store data in a simple text format easy to read and write.

Use JsonSerializer to convert between objects and JSON strings.

Read JSON from files with File.ReadAllText and write with File.WriteAllText.