Home > front end >  Deserializing json property with attribute returns null...C#
Deserializing json property with attribute returns null...C#

Time:03-02

Using NewtonSoft to do the deserializing.

I have the following JSON string

{\"PK\":\"[email protected]\",\"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\":\"[email protected]\",\"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?

CodePudding user response:

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; }
  •  Tags:  
  • c#
  • Related