Reputation: 399
I have a POCO object like this -
class User
{
string FullName { get; set;}
DateTime DOJ { get; set;}
string UserName { get; set;}
}
I have a WebAPI that sends following JSON to update user
PUT /user/{user-id}
{
"FullName ": "My Name",
"DOJ": "01-05-2018",
"UserName": "My_user_Name"
}
// Deserialize in C# code
var user = JsonConvert.DeserializeObject<User>(Above-Json-String);
When i deserialize this json using JSON.net apis, value for "user.UserName" is "My user Name", the underscores got converted into space.
Any solution to preserve underscores in the property value?
Upvotes: 3
Views: 3406
Reputation: 818
Jsonproperty might solve your issue stated.
Use the JsonProperty attribute to indicate the name in the JSON.
[JsonProperty(PropertyName = "binding type")]
string FullName { get; set;}
Upvotes: 3