Core C#

Serialize and Deserialize JSON

This C# program demonstrates how to deserialize and serialize JSON in C#.

Program.cs

using System;
using System.Text.Json;
// Need to add two references
// Right-click "References" in right-hand Solution Explorere pane and select "Add Reference..." and choose "Assemblies->Extensions->System.Text.Json"
// Right-click "References" in right-hand Solution Explorere pane and select "Add Reference..." and choose "Assemblies->Extensions->System.Memory"

namespace SimpleJson
{
    class CTestJson
    {
        public int miID { get; set; }
        public bool mbOpen { get; set; }
        public string msName { get; set; }
        public string[] msaColumns { get; set; }
        public double[][] mdaaTable { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // A test JSON string for a CTestJson object
            string sJsonString = "{" +
                "\"miID\":29," +
                "\"mbOpen\":true," +
                "\"msName\":\"XoaX.net\"," +
                "\"msaColumns\":[\"Sales\", \"Profit\"]," +
                "\"mdaaTable\":[[100000.00,5000.00],[30000.00,2000.00],[200000.00,7000.00]]" +
                "}";
            // Deserialize
            CTestJson qJsonObject = JsonSerializer.Deserialize<CTestJson>(sJsonString);
            // Serialize
            string sJson = JsonSerializer.Serialize(qJsonObject);
            Console.WriteLine("Serialized: " + sJson);
        }
    }
}
 

Output

Serialized: {"miID":29,"mbOpen":true,"msName":"XoaX.net","msaColumns":["Sales","Profit"],"mdaaTable":[[100000,5000],[30000,2000],[200000,7000]]}

Press any key to close this window . . .
 
 

© 2007–2025 XoaX.net LLC. All rights reserved.