How to deserialize JSON object, grouping it's properties into different type in .NET Core?

Viewed 81

I want to retrieve some football data from an external api. Response from the requests returns me "teams" table as shown below:

    "teams": [
        {
            "team_id": 2,
            "name": "France",
            "code": null,
            "logo": null,
            "country": "France",
            "is_national": true,
            "founded": 1919,
            "venue_name": "Stade de France",
            "venue_surface": "grass",
            "venue_address": "Rue Jules Rimet, Saint-Denis",
            "venue_city": "Paris",
            "venue_capacity": 81338
        },
        .....

The team contains 5 venue fields, and i would like to group them into another class aggregated by the team. Here's the code:

public class TeamEntity
{
    [Key]
    public int TeamId { get; set; }
    [Required]
    [MaxLength(100)]
    public string Name { get; set; }
    [MaxLength(10)]
    [Column(TypeName = "varchar(10)")]
    public string Code { get; set; }
    [Required]
    [MaxLength(300)]
    public string Logo { get; set; }
    [Required]
    [MaxLength(100)]
    public string Country { get; set; }
    public string IsNational { get; set; }
    [Required]
    public int Founded { get; set; }
    [Required]
    public VenueEntity Venue { get; set; }
    [ForeignKey("Venue")]
    public int VenueId { get; set; }
    public LeagueEntity League { get; set; }
    [ForeignKey("League")]
    public int LeagueId { get; set; }
}


public class VenueEntity
{
    [Key]
    public int VenueId { get; set; }
    [Required]
    [MaxLength(200)]
    public string VenueName { get; set; }
    [Required]
    [MaxLength(100)]
    public string VenueSurface { get; set; }
    [Required]
    [MaxLength(200)]
    public string VenueAddress { get; set; }
    [Required]
    [MaxLength(100)]
    public string VenueCity { get; set; }
    [Required]
    public int VenueCapacity { get; set; }
    [Required]
    public TeamEntity Team { get; set; }
    [ForeignKey("Team")]
    public int TeamId { get; set; }
}

When i am trying to deserialize it i get an error. I checked using the debugger that Venue field in TeamEntity class is null after deserialization. Is there a way to deserialize object properly with Newtonsoft.Json.JsonConverter? Thanks for any help.

0 Answers
Related