Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to serialize my json so it comes out like this
{
  "order": [
    {
      "ascending": true,
      "field": "string"
    }
  ],
  "groups": [
    {
      "ascending": true,
      "field": "string"
    }
  ],
  "start": 0,
  "limit": 0
}



I am trying to set the member properties and the values as well in one class called DiscoveryModel. I am confused on how to set the <list> values. This is what i have so far

public class DiscoveryModel
   {
          public static DiscoveryModel GetModelForUpdate()
       {
           return new DiscoveryModel
           {
              order = new List<Order> { ascending  = true, field = "string"}





           };
       }


       public List<Order> order { get; set; }

       public List<Group> groups { get; set; }

       public int start { get; set; }

       public int limit { get; set; }

       public class Order
       {

           public bool ascending { get; set; }

           public string field { get; set; }
       }

       public class Group
       {


           public bool ascending { get; set; }

           public string field { get; set; }
       }




}

What I have tried:

I am trying to add the payload and return to the method GetModelForUpdate.
Posted
Updated 24-Mar-20 21:12pm
v3
Comments
Tomas Takac 24-Mar-20 6:22am    
And where is the problem?
Member 14779968 25-Mar-20 2:16am    
So i am facing a problem assigning the values for the List. I am not sure how to do it. I have updated the question. I am not able to assign the member properties ascending and field
ZurdoDev 24-Mar-20 8:46am    
And what happens?

1 solution

Here is the C# representation of your Json payload that can be used to de-serialize automatically with Newtonsoft Json or any other library.

I think you should keep your other logic separate from de-serialization

C#
public class Order
{
    public bool ascending { get; set; }
    public string field { get; set; }
}

public class Group
{
    public bool ascending { get; set; }
    public string field { get; set; }
}

public class DiscoveryModel
{
    public List<Order> order { get; set; }
    public List<Group> groups { get; set; }
    public int start { get; set; }
    public int limit { get; set; }
}


You can create a function to get new model initialized like this

C#
public DiscoveryModel GetModelForUpdate()
{
   return new DiscoveryModel()
   {
      order = new List<Order>() { new Order() { ascending = true, field = "string" } },
      groups = new List<Group>() { new Group() { ascending = true, field = "string" } },
      start = 1,
      limit = 10,
   };
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900