在VB.NET中反序列化这个json

时间:2016-12-06 13:41:40

标签: json vb.net json-deserialization

我有这个json回复:

  {

    "tracked_until": "1483704963",
    "solo_competitive_rank": "4066",
    "competitive_rank": "3821",
    "mmr_estimate": {
        "estimate": 3971,
        "stdDev": 215.26495302301302,
        "n": 20
    },
    "profile": {
        "account_id": 131505839,
        "personaname": "LeG",
        "name": null,
        "cheese": 1,
        "steamid": "76561198091771567",
        "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c0/c09ca9b316ff7bf7dccba6f5a32aba97b8dba05c.jpg",
        "avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c0/c09ca9b316ff7bf7dccba6f5a32aba97b8dba05c_medium.jpg",
        "avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c0/c09ca9b316ff7bf7dccba6f5a32aba97b8dba05c_full.jpg",
        "profileurl": "http://steamcommunity.com/id/LegLabs/",
        "last_login": "2016-11-11T13:13:18.651Z",
        "loccountrycode": "AL"
    }
}

使用在线工具,我创建了这些类:

<Serializable>
    Public Class mmr_estimate
        Public Property estimate As String
        Public Property stdDev As String
        Public Property n As String
    End Class

    <Serializable>
    Public Class profile
        Public Property account_id As String
        Public Property personaname As String
        Public Property name As String
        Public Property cheese As String
        Public Property steamid As String
        Public Property avatar As String
        Public Property avatarmedium As String
        Public Property avatarfull As String
        Public Property profileurl As String
        Public Property last_login As String
        Public Property loccountrycode As String
    End Class

    <Serializable>
    Public Class RootObject
        Public Property tracked_until As String
        Public Property solo_competitive_rank As String
        Public Property competitive_rank As String
        Public Property mmr_estimate As mmr_estimate
        Public Property profile As profile
    End Class

然后我使用此代码对其进行反序列化:

Dim steamData As String = ' the json contents above
Dim myjss As New JavaScriptSerializer()
Dim playerDictionary = myjss.Deserialize(Of List(Of RootObject))(steamData)

但是我得到的结果是什么,playerDictionary有0项,当它应该有1个项目,其中json的内容被解析为KeyValuePairs。

如果我使用这段代码

Dim data = myjss.DeserializeObject(steamData)

然后为数据元素上的每个循环运行一个,我可以在调试时看到数据的内容,但我不知道如何使用它们,因为它们只是我遇到问题的对象转换为KeyValuePairs,它本身可能包含KeyValuePairs数组。

我想要得到的是solo_competitive_rank,competitive_rank和steamid的值,但是如果我不能将所有内容反序列化,我就不能这样做。

声明的类是错误的吗?

1 个答案:

答案 0 :(得分:0)

但是使用此解决方案,您不使用类RootObject。 使用第一种方法,您的JSON需要在列表中包含键和值。 这仅适用于JSON阵列。 :(

RG