Search code examples
c#jsonasp.net-mvcjson-deserializationjsonmodel

Json to C# models problem a class always returns null


Here is my json example:

{
    "generated_at": "2020-11-24T11:27:57+00:00",
    "summaries": [
        {
            "sport_event": {
                "id": "sr:sport_event:22762631",
                "start_time": "2020-11-24T10:00:00+00:00",
                "start_time_confirmed": true,
                "sport_event_context": {
                    "sport": {
                        "id": "sr:sport:1",
                        "name": "Futbol"
                    },
                    "category": {
                        "id": "sr:category:393",
                        "name": "Uluslararas\u0131 Kul\u00fcpler"
                    },
                    "competition": {
                        "id": "sr:competition:463",
                        "name": "AFC \u015eampiyonlar Ligi"
                    },
                    "season": {
                        "id": "sr:season:74427",
                        "name": "AFC \u015eampiyonlar Ligi 2020",
                        "start_date": "2020-01-14",
                        "end_date": "2020-12-19",
                        "year": "2020",
                        "competition_id": "sr:competition:463"
                    },
                    "stage": {
                        "order": 3,
                        "type": "league",
                        "phase": "regular season",
                        "start_date": "2020-02-11",
                        "end_date": "2020-12-03",
                        "year": "2020"
                    },
                    "round": {
                        "number": 4
                    },
                    "groups": [
                        {
                            "id": "sr:league:49469",
                            "name": "AFC Champions League 2020, Group E",
                            "group_name": "E"
                        }
                    ]
                },
                "coverage": {
                    "type": "sport_event",
                    "sport_event_properties": {
                        "lineups": true,
                        "extended_player_stats": false,
                        "extended_team_stats": false,
                        "lineups_availability": "pre",
                        "ballspotting": true,
                        "commentary": true,
                        "fun_facts": true,
                        "goal_scorers": true,
                        "scores": "live",
                        "game_clock": true,
                        "deeper_play_by_play": true,
                        "deeper_player_stats": true,
                        "deeper_team_stats": true,
                        "basic_play_by_play": true,
                        "basic_player_stats": true,
                        "basic_team_stats": true
                    }
                },
                "competitors": [
                    {
                        "id": "sr:competitor:3376",
                        "name": "Beijing Guoan",
                        "country": "\u00c7in",
                        "country_code": "CHN",
                        "abbreviation": "BJG",
                        "qualifier": "home",
                        "gender": "male"
                    },
                    {
                        "id": "sr:competitor:5970",
                        "name": "Melbourne Victory FC",
                        "country": "Avustralya",
                        "country_code": "AUS",
                        "abbreviation": "MEV",
                        "qualifier": "away",
                        "gender": "male"
                    }
                ],
                "venue": {
                    "id": "sr:venue:54139",
                    "name": "Education City Stadium",
                    "capacity": 45350,
                    "city_name": "Al Rayyan",
                    "country_name": "Katar",
                    "map_coordinates": "25.310846, 51.424414",
                    "country_code": "QAT"
                },
                "sport_event_conditions": {
                    "referees": [
                        {
                            "id": "sr:referee:2101998",
                            "name": "Al-Abda, Saoud",
                            "type": "main_referee"
                        }
                    ],
                    "ground": {
                        "neutral": true
                    }
                }
            },
            "sport_event_status": {
                "status": "live",
                "match_status": "2nd_half",
                "home_score": 2,
                "away_score": 0,
                "period_scores": [
                    {
                        "home_score": 2,
                        "away_score": 0,
                        "type": "regular_period",
                        "number": 1
                    },
                    {
                        "home_score": 0,
                        "away_score": 0,
                        "type": "regular_period",
                        "number": 2
                    }
                ],
                "ball_locations": [
                    {
                        "order": 4,
                        "x": 24,
                        "y": 25,
                        "qualifier": "home"
                    },
                    {
                        "order": 3,
                        "x": 28,
                        "y": 54,
                        "qualifier": "home"
                    },
                    {
                        "order": 2,
                        "x": 31,
                        "y": 72,
                        "qualifier": "home"
                    },
                    {
                        "order": 1,
                        "x": 34,
                        "y": 75,
                        "qualifier": "home"
                    }
                ],
                "clock": {
                    "played": "70:17"
                }
            },
            "statistics": {
                "totals": {
                    "competitors": [
                        {
                            "id": "sr:competitor:3376",
                            "name": "Beijing Guoan",
                            "abbreviation": "BJG",
                            "qualifier": "home",
                            "statistics": {
                                "yellow_cards": 1,
                                "yellow_red_cards": 0,
                                "red_cards": 0,
                                "corner_kicks": 3,
                                "shots_total": 7,
                                "shots_on_target": 4,
                                "shots_off_target": 2,
                                "shots_blocked": 1,
                                "ball_possession": 65,
                                "free_kicks": 13,
                                "offsides": 1,
                                "goal_kicks": 4,
                                "throw_ins": 13,
                                "shots_saved": 0,
                                "fouls": 9,
                                "injuries": 1,
                                "cards_given": 1,
                                "substitutions": 3
                            },
                            "players": [
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 0,
                                        "substituted_out": 0,
                                        "offsides": 0,
                                        "corner_kicks": 0,
                                        "shots_on_target": 0,
                                        "shots_off_target": 0,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:328303",
                                    "name": "Sen, Hou",
                                    "starter": true
                                },
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 0,
                                        "substituted_out": 0,
                                        "offsides": 0,
                                        "corner_kicks": 0,
                                        "shots_on_target": 0,
                                        "shots_off_target": 0,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:1272814",
                                    "name": "Kim, Min-Jae",
                                    "starter": true
                                },
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 0,
                                        "substituted_out": 0,
                                        "offsides": 0,
                                        "corner_kicks": 0,
                                        "shots_on_target": 0,
                                        "shots_off_target": 0,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:261597",
                                    "name": "Yang, Yu",
                                    "starter": true
                                },
                                {
                                    "statistics": {
                                        "goals_scored": 1,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 1,
                                        "substituted_in": 0,
                                        "substituted_out": 0,
                                        "offsides": 1,
                                        "corner_kicks": 0,
                                        "shots_on_target": 1,
                                        "shots_off_target": 1,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:34152",
                                    "name": "Renato Augusto",
                                    "starter": true
                                },
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 0,
                                        "substituted_out": 1,
                                        "offsides": 0,
                                        "corner_kicks": 1,
                                        "shots_on_target": 0,
                                        "shots_off_target": 0,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:231992",
                                    "name": "Xizhe, Zhang",
                                    "starter": true
                                },
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 0,
                                        "substituted_out": 0,
                                        "offsides": 0,
                                        "corner_kicks": 0,
                                        "shots_on_target": 0,
                                        "shots_off_target": 0,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:59872",
                                    "name": "Dabao, Yu",
                                    "starter": false
                                },
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 0,
                                        "substituted_out": 0,
                                        "offsides": 0,
                                        "corner_kicks": 0,
                                        "shots_on_target": 0,
                                        "shots_off_target": 0,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:1418851",
                                    "name": "Fan, Yang",
                                    "starter": false
                                },
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 0,
                                        "substituted_out": 0,
                                        "offsides": 0,
                                        "corner_kicks": 0,
                                        "shots_on_target": 0,
                                        "shots_off_target": 0,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:223230",
                                    "name": "Peng, Lu",
                                    "starter": false
                                },
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 0,
                                        "substituted_out": 0,
                                        "offsides": 0,
                                        "corner_kicks": 0,
                                        "shots_on_target": 0,
                                        "shots_off_target": 0,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:328311",
                                    "name": "Tao, Jiang",
                                    "starter": false
                                },
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 1,
                                        "substituted_out": 0,
                                        "offsides": 0,
                                        "corner_kicks": 0,
                                        "shots_on_target": 0,
                                        "shots_off_target": 0,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:815216",
                                    "name": "Dun, BA",
                                    "starter": false
                                }
                            ]
                        },
                        {
                            "id": "sr:competitor:5970",
                            "name": "Melbourne Victory FC",
                            "abbreviation": "MEV",
                            "qualifier": "away",
                            "statistics": {
                                "yellow_cards": 1,
                                "yellow_red_cards": 0,
                                "red_cards": 0,
                                "corner_kicks": 3,
                                "shots_total": 6,
                                "shots_on_target": 0,
                                "shots_off_target": 4,
                                "shots_blocked": 2,
                                "ball_possession": 35,
                                "free_kicks": 10,
                                "offsides": 3,
                                "goal_kicks": 7,
                                "throw_ins": 21,
                                "shots_saved": 2,
                                "fouls": 10,
                                "injuries": 0,
                                "cards_given": 1,
                                "substitutions": 4
                            },
                            "players": [
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 0,
                                        "substituted_out": 0,
                                        "offsides": 0,
                                        "corner_kicks": 0,
                                        "shots_on_target": 0,
                                        "shots_off_target": 2,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:155973",
                                    "name": "Ansell, Nick",
                                    "starter": true
                                },
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 0,
                                        "substituted_out": 1,
                                        "offsides": 0,
                                        "corner_kicks": 0,
                                        "shots_on_target": 0,
                                        "shots_off_target": 0,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:1436448",
                                    "name": "Kirdar, Birkan",
                                    "starter": true
                                },
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 0,
                                        "substituted_out": 0,
                                        "offsides": 0,
                                        "corner_kicks": 0,
                                        "shots_on_target": 0,
                                        "shots_off_target": 0,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:122551",
                                    "name": "Traore, Adama",
                                    "starter": true
                                },
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 0,
                                        "substituted_out": 0,
                                        "offsides": 3,
                                        "corner_kicks": 0,
                                        "shots_on_target": 0,
                                        "shots_off_target": 0,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:122374",
                                    "name": "Rojas, Marco",
                                    "starter": true
                                },
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 0,
                                        "substituted_out": 1,
                                        "offsides": 0,
                                        "corner_kicks": 0,
                                        "shots_on_target": 0,
                                        "shots_off_target": 0,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:1270838",
                                    "name": "Folami, Ben",
                                    "starter": true
                                },
                                {
                                    "statistics": {
                                        "goals_scored": 0,
                                        "yellow_cards": 0,
                                        "yellow_red_cards": 0,
                                        "red_cards": 0,
                                        "own_goals": 0,
                                        "assists": 0,
                                        "substituted_in": 1,
                                        "substituted_out": 0,
                                        "offsides": 0,
                                        "corner_kicks": 0,
                                        "shots_on_target": 0,
                                        "shots_off_target": 0,
                                        "shots_blocked": 0
                                    },
                                    "id": "sr:player:1732609",
                                    "name": "Anderson, Aaron",
                                    "starter": false
                                }
                            ]
                        }
                    ]
                }
            }
        }

Hello guys, i tried to convert json to c# model on internet sites that converts online, but i can't react the statistic of players or teams. It returns always null. How can i generate this json to c# model correctly to c# classes? I can't get any statistics of teams or players. Thank you for helping me Regards,


Solution

  • There is 3 differents statistics with different properties. While for you it's logic that it's the same entity. Why whould a tool assume that it's the same.

    You have 2 solution depending on how you view those stats. Either it's a class with a limited set of properties? And you past all those properties in the same class and eliminate the duplicate.

    Or Statistic is a crazy unbound set of name and value. And you can't know what will be the next stats "Number of player with bleu eyes"

    [JsonProperty("statistics")]   public Dictionary<string, long> Statistics { get; set; }