Reputation: 5866
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
Upvotes: 0
Views: 75
Reputation: 10071
Your JSON does not match your c# classes.
CartDetail is not an array in your JSON but you have declared it as an ICollection in your C# code.
Change your CartDetail from ICollection to CartDetail instead. Also, the c# property name needs to match the JSON property name (if you dont want to use the JsonProperty attribute) so rename CartDetails to CartDetail.
Btw, it seems like you are trying to deserialize directly to some "domain/business classes"? Try creating separate dtos and deserialize to them and then create your business objects from the dtos, it gets much cleaner then :)
So create new classes that you only use for serialization/deserialization and then create a factory or whatever that maps from the new dtos to your business objects. Separation of concerns FTW :).
Upvotes: 2
Reputation: 3439
Simply copy the JSON and use Visual Studio's Paste Special feature:
It will generate the following classes for you:
public class Cart
{
public string accessToken { get; set; }
public string requestId { get; set; }
public string CreatedDate { get; set; }
public Cartdetail CartDetail { get; set; }
}
public class Cartdetail
{
public string ProductId { get; set; }
public string Qty { get; set; }
public string Price { get; set; }
public string CreatedDate { get; set; }
}
Then simply deserialize it:
Upvotes: 2