Avatar ·

How to convert a C# object to a JSON string in .NET?

Hello everyone! I'm new to C# and .NET, and I'm having trouble with serializing objects to JSON.\n\nI need to send data to a REST API that accepts JSON. I have a simple class:\n\n```csharp\npublic class User\n{\n public string Name { get; set; }\n public int Age { get; set; }\n public string Email { get; set; }\n}\n```\n\nI create an instance of this class:\n\n```csharp\nUser user = new User \n{ \n Name = "Ivan Petrov", \n Age = 30, \n Email = "ivan@example.com" \n};\n```\n\nNow I need to convert this object into a JSON string to pass it in an HTTP request. I've heard that .NET has some built-in tools for this, but I don't know how to use them properly.\n\nI tried using `JavaScriptSerializer`, but it's deprecated and not recommended. I tried to