Tuesday, January 8, 2019

How to convert JSON object to class in Asp.Net MVC

I can't convert the JSON object below to class in Asp.Net MVC
{
  "Name.Test":"fsafasfda"
}
Any suggestions are appreciated.
The scenario is like this in asp.net action:
[HttpPut]        
public JsonResult Test(NameRootobject obj)
{

}
the the class was paste JOSN as class:
class NameRootobject 
{

    public string NameTest{get;set;}

}
But the NameTest property is null.
How to resolve this problem?

Answer:
Use the JsonProperty attribute on the class so the framework knows how to deal with that property name
public class NameRootobject {
    [JsonProperty("Name.Test")]
    public string NameTest { get; set; }
}

No comments:

Post a Comment

No String Argument Constructor/Factory Method to Deserialize From String Value

  In this short article, we will cover in-depth the   JsonMappingException: no String-argument constructor/factory method to deserialize fro...