-1

I'm working on serializing an JSON File returned from an API. I have set up the model based on this JSON file and and using Newtonsoft.Json. I am able to successfully deserialize the top level of this file, but am not able to get access to the values of the items off of the first level.

Below is the Json File. My model, and the code that is successfully returning the alias of "MLB". I have no problem returning "Major Leauuge Baseball", "2fa448bc-fc17-4d3d-be03-e60e080fdc26", or "2019-06-12". However when I try to return games and the data under that I'm getting nulls.

Json snippet:

{
    "league":
    {
        "alias": "MLB",
        "name": "Major League Baseball",
        "id": "2fa448bc-fc17-4d3d-be03-e60e080fdc26",
        "date": "2019-06-12",
        "games": [
            {
                "game":
                {
                    "id": "83d1dded-d0c8-480d-acb4-9019afbc43e9",
                    "status": "scheduled",
                    "coverage": "full",
                    "game_number": 1,
                    "day_night": "N",
                    "scheduled": "2019-06-13T00:15:00+00:00",
                    "home_team": "833a51a9-0d84-410f-bd77-da08c3e5e26e",
                    "away_team": "575c19b7-4052-41c2-9f0a-1c5813d02f99",
                    "venue":
                    {
                        "name": "Kauffman Stadium",
                        "market": "Kansas City",
                        "capacity": 37903,
                        "surface": "grass",
                        "address": "One Royal Way",
                        "city": "Kansas City",
                        "state": "MO",
                        "zip": "64129",
                        "country": "USA",
                        "id": "6fca95c9-7f2c-4acb-a9f3-02ef96340d2a"
                    },

Classes:

public class LeaugeResult
{
    public League league { get; set; }
}

public class League
{
    public string alias { get; set; }
    public string name { get; set; }
    public string id { get; set; }
    public string date { get; set; }
    public List<Game> games { get; set; }
}

    public class Game
{
    public string id { get; set; }
    public string status { get; set; }
    public string coverage { get; set; }
    public string game_number { get; set; }
    public string day_night { get; set; }
    public string scheduled { get; set; }
    public string home_team { get; set; }
    public string away_team { get; set; }
    public Venue venue { get; set; }
    public Broadcast broadcast { get; set; }
    public Outcome outcome { get; set; }
    public Home home { get; set; }
}

Method:

        var j = MLBMethods.GetLeaugeResult();

        var alias = j.league.alias;
        Console.WriteLine(alias);

This successfully returns "MLB".

        var gamesList = j.league.games;

        foreach( var game in gamesList)
        {
            Console.WriteLine(game.id);
        }

However this returns nulls for game.id.

Any idea on what I'm doing wrong? I believe I have something in my model set up incorrectly. The part I'm worried about in the Json is below - how do I go about diving into the properties of games/game?

  "games": [
  {
    "game": {
      "id": "83d1dded-d0c8-480d-acb4-9019afbc43e9",
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
smithereens
  • 56
  • 10

1 Answers1

0

games contains a list of objects, and each object has a game property, as opposed to each list item being a Game object.

Because of this, your C# classes don't match with your JSON. You actually need an extra class:

public class GameWrapper
{
    public Game game { get; set; }
}

League should be updated from public List<Game> games { get; set; } to:

public List<GameWrapper> games { get; set; }

And your loop should now select the wrapped Game object (see .Select(g => g.Game):

var gamesList = j.league.games;

foreach( var game in gamesList.Select(g => g.game))
{
    Console.WriteLine(game.id);
}

Short answer: make sure your JSON and C# classes match.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86