Deserializing json property with attribute returns null...C#

Viewed 32

Using NewtonSoft to do the deserializing.

I have the following JSON string

{\"PK\":\"test1@gmail.com\",\"SK\":\"read\",\"Role\":\"RLE#readonly\"}

I want to deserialize it to the following class:

public class UserDto
 {

    [JsonPropertyName("PK")]

    public string Email { get; set; }

    public string SK { get; set; }

     public string Role { get; set; }

 }

When i run the folllowing:

var json = "{\"PK\":\"test1@gmail.com\",\"SK\":\"read\",\"Role\":\"RLE#readonly\"}";
var dto = JsonConvert.DeserializeObject<UserDto>(json);

All properties have values except Email which is null. I have the JsonProperty attribute but its still not working. How do I deserialize the PK JSON property to my Email C# property?

1 Answers

Like i said in the comment on your last post, pretty sure it's you mixing deserializers between system.text and the newtonsoft one. Try

public class UserDto
{        
    [JsonProperty("PK")]

    public string Email { get; set; }
Related