c# - match json data to class properties -


i have used 1 webapi method [frombody] used class object. below:

 public httpresponsemessage processresource([frombody]filecontent contentvalue)      {//some business logic      }

and below json format sending client machine:

 {"filecontent":{"resourcestrings":[{"stringkey":"testkey","stringid":1,"value":"testkey"},{"stringkey":"samplekey","stringid":2,"value":"test key 1"},{"stringkey":"homekey","stringid":3,"value":"home dev"},{"stringkey":"custom.wvf.contactform.name","stringid":4,"value":"name"},{"stringkey":"custom.cms.menuitem","stringid":5,"value":"cms.menuitem"}]},}

below filecontent class used:

public class resourcestring  {      public string stringkey { get; set; }      /// <summary>      /// gets or sets stringkey      /// </summary>      /// <value>      /// stringkey      /// </value>      public int stringid { get; set; }      /// <summary>      /// gets or sets stringid      /// </summary>      /// <value>      /// stringid      /// </value>      public string value { get; set; }      /// <summary>      /// gets or sets value      /// </summary>      /// <value>      /// value      /// </value>  }  /// <summary>  /// added rootobject new class serialize , deserialize resource string  /// </summary>  public class rootobject  {      /// <summary>      /// gets or sets list of resourcestring      /// </summary>      public list<resourcestring> resourcestrings { get; set; }  }  public class filecontent  {      public list<resourcestring> resourcestrings { get; set; }  }

now when sending json data client machine , debugging web api method filecontent object value null.

how json data in method parameter?

now able json data in method using roma's solution. after getting data need again need deserialize resourcestring again should json format below:

 {"resourcestrings":[{"stringkey":"testkey","stringid":1,"value":"testkey"},{"stringkey":"samplekey","stringid":2,"value":"test key 1"},{"stringkey":"homekey","stringid":3,"value":"home dev"},{"stringkey":"custom.wvf.contactform.name","stringid":4,"value":"name"},{"stringkey":"custom.cms.menuitem","stringid":5,"value":"cms.menuitem"}]}

how make deserialize same?

make rootobject:

public class rootobject {     public filecontent filecontent { get; set; } } 

and change action to:

public httpresponsemessage processresource([frombody]rootobject obj) {     //some business logic } 

or

if want parse filecontent json should be:

{"resourcestrings":[{"stringkey":"testkey","stringid":1,"value":"testkey"},{"stringkey":"samplekey","stringid":2,"value":"test key 1"},{"stringkey":"homekey","stringid":3,"value":"home dev"},{"stringkey":"custom.wvf.contactform.name","stringid":4,"value":"name"},{"stringkey":"custom.cms.menuitem","stringid":5,"value":"cms.menuitem"}]} 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -