0

How to convert console output to JSON format output using c# code

    class Program
    {
        static void Main(string[] args)
        {
            Names();
            Console.ReadLine();
        }

        public static void Names()
        {
            List<Name> names = new List<Name>();

            names.Add( new Name() { FName = "Sri",LName="Kumari"});
            names.Add(new Name() { FName = "Pavan", LName = "Kumar" });
            names.Add(new Name() { FName = "Harish", LName = "Raj" });

            foreach (Name item in names)
            {
                Console.WriteLine(item.FName + "\t" + item.LName);
            }
        }
    }

    public class Name
    {
        public string FName { get; set; }
        public string LName { get; set; }
    }

Output :

Sri     Kumari
Pavan   Kumar
Harish  Raj

I want to print that console output into JSON format like the below ex:

{
"Name":
[{"FName":"Sri","LName":"Kumari},{"FName":"Sri","LName":"Kumari},{"FName":"Sri","LName":"Kumari}]
}

I'm new to coding.

Any ideas would be appreciated.

ashok
  • 199
  • 1
  • 12
  • 2
    This answer should help you: https://stackoverflow.com/questions/6201529/how-do-i-turn-a-c-sharp-object-into-a-json-string-in-net – Roberto Conte Rosito Sep 27 '21 at 16:26
  • In the linked q there, just be aware that some of the answers are verrrry ooooold, and we donlt really use what they advocate any more. Modern serializers are Newtonsoft and System.Text.Json, *not* javascriptserializer.. – Caius Jard Sep 27 '21 at 16:32

0 Answers0