[c#] C# JSON 직렬화 예제

JSON(JavaScript Object Notation)은 데이터를 교환하기 위한 경량의 형식입니다. C#에서는 System.Text.Json 네임스페이스를 사용하여 객체를 JSON 형식으로 직렬화할 수 있습니다.

using System;
using System.Text.Json;

namespace JSONSerializationExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // 직렬화할 객체 생성
            var person = new Person
            {
                Name = "John Doe",
                Age = 30,
                Email = "johndoe@example.com"
            };

            // 객체를 JSON 문자열로 직렬화
            string jsonString = JsonSerializer.Serialize(person);
            Console.WriteLine(jsonString);
        }

        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public string Email { get; set; }
        }
    }
}

위 코드는 Person 클래스의 인스턴스를 JSON 문자열로 직렬화하는 간단한 예제를 보여줍니다. 이 예제는 System.Text.Json 네임스페이스를 사용하여 JSON 직렬화를 수행하고, Console.WriteLine을 통해 결과를 출력합니다.

JSON 직렬화를 위한 더 자세한 내용은 Microsoft 공식 문서를 확인하십시오.