I am trying to deserialize json but couldnt get what I want. I would like to deserialize the json to C# object but I couldnt deserialize its children. I would like to share my code and ask for your opinion...
{
"accessToken":"123",
"requestId":"5mRIo6Y6epripoBA3YTM+ZDVgDVR2adB43euMJETguSboG0HT6j7Uje5mU0je5CF",
"CreatedDate":"2019-07-24",
"CartDetail":{
"ProductId":"1",
"Qty":"2",
"Price":"3",
"CreatedDate":"2019-07-24"
}
}
this is the C# code that tries to deserialize
HttpContent requestContent = Request.Content;
string jsonContent = requestContent.ReadAsStringAsync().Result;
Cart cart = JsonConvert.DeserializeObject<Cart>(jsonContent);
This code could deserialize inner object CartDetail. How can I fix this?
This is the Cart class
public partial class Cart
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Cart()
{
this.CartDetails = new HashSet<CartDetail>();
}
public int Id { get; set; }
public string RequestId { get; set; }
public string AccessToken { get; set; }
public Nullable<int> UserId { get; set; }
public System.DateTime CreatedDate { get; set; }
public virtual User User { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<CartDetail> CartDetails { get; set; }
}
This is the CartDetail class
public partial class CartDetail
{
public int Id { get; set; }
public int CartId { get; set; }
public int ProductId { get; set; }
public decimal Qty { get; set; }
public decimal Price { get; set; }
public System.DateTime CreatedDate { get; set; }
public virtual Cart Cart { get; set; }
}
Thanks