How to deserialize JSON with list of different types?

Viewed 449

I'm using 3rd-party API, and one of its methods returns json with list of objects of different types. Like this:

enter image description here

EDIT: Here's the JSON:

  "plays": [
    {
      "description": "Jump Ball Jordan vs. Nurkic: Tip to",
      "jumpBall": {
        "awayPlayer": {
          "id": 9192,
          "firstName": "Jusuf",
          "lastName": "Nurkic",
          "position": "C",
          "jerseyNumber": 27
        },
        "homePlayer": {
          "id": 9268,
          "firstName": "DeAndre",
          "lastName": "Jordan",
          "position": "C",
          "jerseyNumber": 6
        },
        "wonBy": "HOME",
        "tippedToPlayer": null
      },
      "playStatus": {
        "quarter": 1,
        "secondsElapsed": 0,
        "awayPlayersOnCourt": [
          {
            "player": {
              "id": 9192,
              "firstName": "Jusuf",
              "lastName": "Nurkic",
              "position": "C",
              "jerseyNumber": 27
            },
            "location": null
          }
        ],
        "homePlayersOnCourt": [
          {
            "player": {
              "id": 9268,
              "firstName": "DeAndre",
              "lastName": "Jordan",
              "position": "C",
              "jerseyNumber": 6
            },
            "location": null
          }
        ]
      }
    },
    {
      "description": "MISS Rivers 8' Floating Jump Shot",
      "fieldGoalAttempt": {
        "team": {
          "id": 102,
          "abbreviation": "LAC"
        },
        "shootingPlayer": {
          "id": 9270,
          "firstName": "Austin",
          "lastName": "Rivers",
          "position": "PG",
          "jerseyNumber": 25
        },
        "assistingPlayer": null,
        "blockingPlayer": null,
        "shotType": "Floating Jump Shot",
        "distanceFeet": 0,
        "points": 2,
        "location": {
          "x": 126,
          "y": 245
        },
        "result": "MISSED"
      },
      "playStatus": {
        "quarter": 1,
        "secondsElapsed": 16,
        "awayPlayersOnCourt": [],
        "homePlayersOnCourt": [
          {
            "player": {
              "id": 9270,
              "firstName": "Austin",
              "lastName": "Rivers",
              "position": "PG",
              "jerseyNumber": 25
            },
            "location": null
          }
        ]
      }
    },
    {
      "description": "Thornwell REBOUND (Off:1 Def:0)",
      "rebound": {
        "team": {
          "id": 102,
          "abbreviation": "LAC"
        },
        "retrievingPlayer": {
          "id": 13857,
          "firstName": "Sindarius",
          "lastName": "Thornwell",
          "position": "PG",
          "jerseyNumber": null
        },
        "type": "OFFENSIVE"
      },
      "playStatus": {
        "quarter": 1,
        "secondsElapsed": 19,
        "awayPlayersOnCourt": [],
        "homePlayersOnCourt": [
          {
            "player": {
              "id": 13857,
              "firstName": "Sindarius",
              "lastName": "Thornwell",
              "position": "PG",
              "jerseyNumber": null
            },
            "location": null
          }
        ]
      }
    }
  ],

As you can see, each type has description and playStatus, but also specific type, like jumpBall or Rebound. How do I deserialize such list to C# object? Obviously jumpBall has not much in common with fieldGoalAttempt.

My solution:

The only idea that comes to mind, is to create a Play object, like this:

public class Play
{
    public string Name { get; set; } // JumpBall, Rebound, etc... 
    public object PlayObject { get; set; } // The actual play object
}

Then.. if I want to use Play in the code, I would cast PlayObject to the type based on the Name.

Questions:

Is this a good strategy? If so, How do I deserialize such json to the list of Play in C#?

If this is not a good idea, how should I approach this situation?

0 Answers
Related