C# Lists to JSON

Anonymous
2022-09-06T05:11:36.817+00:00

I defined a Class

  public class Product  
    {  
        public int Id { get; set; }  
        public string Name { get; set; }  
        public string position { get; set; }  
    public List<string> playerSkills  { get; set; }  
    }  

Here is source code from webapi(entire sample here https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api)

Product[] products = new Product[]
{
new Product { Id = 1, Name = "Player1", position = "Defender" , playerSkills = new List<string>(){ "id=1","skill=defense","value=60","playerId=1" } },
new Product { Id = 2, Name = "Player2", position = "Midfielder" , playerSkills = new List<string>(){ "id=3","skill=attack","value=20","playerId=2" } },

    };  

    public IEnumerable<Product> GetAllProducts()  
    {  
        return products;  
    }  

Actual Output
[{"Id":1,"Name":"Player1","position":"Defender","playerSkills":["id=1","skill=defense","value=60","playerId=1"]},{"Id":2,"Name":"Player2","position":"Midfielder","playerSkills":["id=3","skill=attack","value=20","playerId=2"]}]

But I like to accommodate multiple player skills for a player

Desired Output

[
{
"id": 1,
"name": "player name 1",
"position": "defender",
"playerSkills": [
{
"id": 1,
"skill": "defense",
"value": 60,
"playerId": 1
},
{
"id": 2,
"skill": "speed",
"value": 80,
"playerId": 1
}
]
},
{
"id": 2,
"name": "player name 2",
"position": "midfielder",
"playerSkills": [
{
"id": 3,
"skill": "attack",
"value": 20,
"playerId": 2
},
{
"id": 4,
"skill": "speed",
"value": 70,
"playerId": 2
}
]
}
]

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2022-09-06T08:31:06.83+00:00

    Try something like this:

    public class Product  
    {  
        public int Id { get; set; }  
        public string Name { get; set; }  
        public string position { get; set; }  
        public List<Skill> playerSkills { get; set; }  
    }  
      
    public class Skill  
    {  
        public int id { get; set; }  
        public string skill { get; set; }  
        public string value { get; set; }  
        public int playerId { get; set; }  
    }  
      
    . . .  
      
    Product[] products = new Product[]  
    {  
        new Product { Id = 1, Name = "Player1", position = "Defender" ,  
            playerSkills = new List<Skill> {  
                new Skill { id = 1, skill = "defense", value = "60", playerId = 1 },  
                new Skill { id = 1, skill = "speed", value = "80", playerId = 1 } } },  
        new Product { Id = 2, Name = "Player2", position = "Midfielder",  
            playerSkills = new List<Skill> { new Skill { id=3,skill = "attack", value = "20", playerId=2 } } },  
    };  
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.