簡體   English   中英

在VB.NET中反序列化此json

[英]Deserialize this json in VB.NET

我有這個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的值,但是如果無法對整個內容進行反序列化,則無法這樣做。

聲明的類錯誤嗎?

但是使用此解決方案,您不使用類RootObject。 使用第一種方法時,您的JSON要在列表中包含鍵和值。 這僅適用於JSON數組。 :(

RG

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM