23
1
using System;
2
using System.Text.Json;
3
4
namespace ObjectToJSONConversion
5
{
6
public class Department
7
{
8
public int DeptId { get; set; }
9
public string DepartmentName { get; set; }
10
}
11
12
class Program
13
{
14
public static void Main()
15
{
16
Department dept= new Department() { DeptId = 101, DepartmentName= "IT" };
17
18
byte[] utf8bytesJson = JsonSerializer.SerializeToUtf8Bytes(dept);
19
string strJson = System.Text.Encoding.UTF8.GetString(utf8bytesJson);
20
Console.WriteLine(strJson);
21
}
22
}
23
}
Cached Result
Compilation error (line 1, col 1): A namespace cannot directly contain members such as fields or methods