用于反序列化动态JSON数据的类

时间:2019-12-16 18:31:13

标签: json vb.net deserialization

我正在尝试构建用于反序列化一些动态/更改JSON数据的类结构,但是经过多次尝试,我无法找到一种创建类声明的方法来使用System.Runtime.Serialization提取此数据。杰森。

这是引起我悲伤的JSON部分:

"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"
            }
        }
    },

“点数”数据中的唯一ID引起了我的不满(即1336、1337、1338等)。我可以在下面创建一个像这样的类结构,以将其插入并且可以正常工作。我的问题是ID每周都会更改,并且用一年中可能使用的每个可能的数字来创建GamesPick类都没有道理。

是否可以使用System.Runtime.Serialization.Json创建/声明一个(或多个)类来动态提取此数据,或者我将不得不使用JSon手动解析它。 NET库?

  <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

编辑: 我已经按照建议(在此处)更新了类:

    <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

但是当我反序列化JSON时,任何专家都没有选择:

enter image description here

0 个答案:

没有答案