This is my Class for my ROOT object
public class Root
{
public List
}
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
{
new Root
{
Positions = new List
{
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

Amit MohantyRank #112 · 20K reputationPosted Jul 11, 2023, 3:40 AM EDT
Check this:
public class Root { public List? Positions { get; set; } } public class Position { [JsonProperty("SerialNumber", NullValueHandling = NullValueHandling.Ignore)] public string SerialNumber { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public int? position { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public int? result { get; set; } }var root = new Root { Positions = new List { new Position { SerialNumber = "34543545" }, new Position { position = 1, result = 0 }, new Position { position = 2, result = 0 }, new Position { position = 3, result = 0 } } }; var results = new List { root }; string json = JsonConvert.SerializeObject(results, Formatting.Indented); Console.WriteLine(json);Jignesh KumarRank #91 · 22.9K reputationPosted Jul 10, 2023, 11:07 PM EDT
Hello,
Please try this one,
public class Root { public string? SerialNumber { get; set; } public List? Positions { get; set; } } public class Position { public int? position { get; set; } public int? result { get; set; } }Drt82 reputationPosted Jul 10, 2023, 11:21 PM EDT
Hi Jignesh,
Already I tired the above but am getting result like below but i want all the content under Positions
[
{
"SerialNumber ": "34543545",
"positions": [
{
"position": 1,
"result": 0
},
{
"position": 2,
"result": 1
},
{
"position": 3,
"result": 0
}
]
}
]
But i want the format like below
[{
"Positions": [
{
"panelSn": "34543545"
},
{
"position": 1,
"result": 0
},
{
"position": 2,
"result": 0
},
{
"position": 3,
"result": 0
}
]
}]