1

I'm trying to build the class structure for deserializing some dynamic/changing JSON data but after many attempts I'm not able to figure out a way to create the class declarations to pull this data in using System.Runtime.Serialization.Json.

Here's the portion of the JSON that's causing me grief:

"expertPicks": [
    {
        "user_id": 280,
        "name": "Andrew Brandt",
        "picks": {
            "1336": {
                "st": 1,
                "s": 1,
                "t": "GB",
                "r": "win"
            },
            "1337": {
                "st": 1,
                "s": 7,
                "t": "TEN",
                "r": "win"
            },
            "1338": {
                "st": 1,
                "s": 3,
                "t": "BAL",
                "r": "win"
            },
            "1339": {
                "st": 1,
                "s": 4,
                "t": "BUF",
                "r": "win"
            },
            "1340": {
                "st": 1,
                "s": 8,
                "t": "PHI",
                "r": "win"
            },
            "1341": {
                "st": 1,
                "s": 6,
                "t": "LAR",
                "r": "win"
            },
            "1344": {
                "st": 1,
                "s": 2,
                "t": "MIN",
                "r": "win"
            },
            "1347": {
                "st": 1,
                "s": 5,
                "t": "KC",
                "r": "win"
            },
            "1351": {
                "st": 1,
                "s": 9,
                "t": "SEA",
                "r": "win"
            },
            "1352": {
                "st": 1,
                "s": 10,
                "t": "LAC",
                "r": "win"
            },
            "1353": {
                "st": 1,
                "s": 11,
                "t": "AZ",
                "r": "tie"
            },
            "1354": {
                "st": 1,
                "s": 12,
                "t": "DAL",
                "r": "win"
            },
            "1355": {
                "st": 1,
                "s": 13,
                "t": "TB",
                "r": "lose"
            },
            "1356": {
                "st": 1,
                "s": 14,
                "t": "NE",
                "r": "win"
            },
            "1357": {
                "st": 1,
                "s": 15,
                "t": "NO",
                "r": "win"
            },
            "1358": {
                "st": 1,
                "s": 16,
                "t": "OAK",
                "r": "win"
            }
        }
    },

It's the unique ID's in the "picks" data that's causing me grief (i.e. 1336, 1337, 1338, etc.). I can create a class structure like this one below to pull them in and that works fine. My problem is that the IDs will change every week, and it doesn't make sense to create a GamesPick class with every possible number they might use through the year.

Is there any way at all that I can create/declare a class (or classes) to dynamically pull this data in using System.Runtime.Serialization.Json, or am I going to have to parse it manually using the JSon.NET library?

  <DataContract()> _
    Public Class ExpertPick
        <DataMember(name:="user_id")> _
        Public Property ExpertID As Integer
        <DataMember(name:="name")> _
        Public Property ExpertName As String
        <DataMember(name:="picks")> _
        Public Property Picks As GamePicks
    End Class
   <DataContract()> _
    Public Class GamePicks
        <DataMember(Name:="1336")> _
        Public Property Game1336 As GamePick
        <DataMember(Name:="1337")> _
        Public Property Game1337 As GamePick
        <DataMember(Name:="1338")> _
        Public Property Game1338 As GamePick
        <DataMember(Name:="1339")> _
        Public Property Game1339 As GamePick
        <DataMember(Name:="1340")> _
        Public Property Game1340 As GamePick
        <DataMember(Name:="1341")> _
        Public Property Game1341 As GamePick
        <DataMember(Name:="1344")> _
        Public Property Game1344 As GamePick
        <DataMember(Name:="1347")> _
        Public Property Game1347 As GamePick
        <DataMember(Name:="1351")> _
        Public Property Game1351 As GamePick
        <DataMember(Name:="1352")> _
        Public Property Game1352 As GamePick
        <DataMember(Name:="1353")> _
        Public Property Game1353 As GamePick
        <DataMember(Name:="1354")> _
        Public Property Game1354 As GamePick
        <DataMember(Name:="1355")> _
        Public Property Game1355 As GamePick
        <DataMember(Name:="1356")> _
        Public Property Game1356 As GamePick
        <DataMember(Name:="1357")> _
        Public Property Game1357 As GamePick
        <DataMember(Name:="1358")> _
        Public Property Game1358 As GamePick
    End Class

Edit: I've updated the classes as suggested (here):

    <DataContract()> _
    Public Class ExpertPick
        <DataMember(name:="user_id")> _
        Public Property ExpertID As Integer
        <DataMember(name:="name")> _
        Public Property ExpertName As String
        <DataMember(name:="picks")> _
        Public Property Picks As Dictionary(Of String, GamePick)
    End Class
    <DataContract()> _
    Public Class GamePick
        <DataMember(Name:="t")> _
        Public Property Team As String
        <DataMember(Name:="st")> _
        Public Property st As Integer
        <DataMember(Name:="s")> _
        Public Property s As Integer
        <DataMember(Name:="r")> _
        Public Property r As String
    End Class

But when I deserialize the JSON I get no picks for any of the experts:

enter image description here

  • Make `Picks` be a `Dictionary(Of Long, GamePick)` or `Dictionary(Of String, GamePick)` as shown in [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182) or [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/a/34213724/3744182). Those questions are for c# but the answer is the same. – dbc Dec 16 '19 at 18:38
  • 1
    Picks should be a `Dictionary(Of String, [class])`, where `class` is a Class that defines `st`, `s`, `t` and `r` properties – Jimi Dec 16 '19 at 18:39
  • And here's a good vb.net specific duplicate: [Generating Class VB.NET](https://stackoverflow.com/q/45789578/3744182). – dbc Dec 16 '19 at 18:43

0 Answers0