Drt

Drt

  • 1.6k
  • 82
  • 27.6k

JSON format to add values into list in c#

Jul 11 2023 2:16 AM

This is my Class for my ROOT object

public class Root
    {
        public List<Position>? Positions { get; set; }
    }

    public class Position
    {
        public string? SerialNumber { get; set; }
        public int? position { get; set; }
        public int? result { get; set; }
    }

 

When I tried to add a hardcode values like below

     public static List<Root> Results = new List<Root>()
        {
             new Root
             {
                Positions = new List<Position>()
                 {
                    new Position
                    {
                        SerialNumber ="34543545",
                        position = 1,
                        result= 0,
                    },
                    new Position
                    {
                        SerialNumber ="34543545",
                        position = 2,
                        result= 0,
                    },
                    new Position
                    {
                        SerialNumber ="34543545",
                        position = 3,
                        result= 0,
                    }
                }
             }
        };

 but i  need to add the values  like below json format please help me how to add into list like below format

[{
    "Positions": [
        {
            "SerialNumber ": "34543545"
        },
        {
            "position": 1,
            "result": 0
        },
        {
            "position": 2,
            "result": 0
        },
        {
            "position": 3,
            "result": 0
        }
    ]
}]

PLz help
 


Answers (3)