How to use Json.stringfy() to override toString() in C# class

  • Thread starter Thread starter zydjohn
  • Start date Start date
Z

zydjohn

Guest
I have the following C# class:



public class RootObject
{
public string Name { get; set; }
public int Age { get; set; }
public string City { get; set; }
}


I want to override the toString() to build a Json object of the class, just like the JavaScript’s example:

var obj = { Name: "John", Age: 30, City: "New York" };

var myJSON = JSON.stringify(obj);

I want to use built-in System.Text.Json to do this, but I can’t figure it out. The RootObject may contain complicated data structure, not just as in my example, only has simple data type.

Please advice.

By the way, I am using Visual Studio 2019 Version 16.3.8 on Windows 10 (Version 1903).

Continue reading...
 
Back
Top