如何使用Json Api解析动态Json字符串

时间:2015-07-05 04:38:21

标签: json vb.net

我正在尝试使用来自的API将此String解析为我的程序  http://www.newtonsoft.com/json/help/html/Introduction.htm

{
"193461232": [
    {
        "name": "Master Yi's Pyromancers",
        "tier": "GOLD",
        "queue": "RANKED_SOLO_5x5",
        "entries": [
            {
                "playerOrTeamId": "193461232",
                "playerOrTeamName": "wamborambo",
                "division": "V",
                "leaguePoints": 58,
                "wins": 109,
                "losses": 104,
                "isHotStreak": false,
                "isVeteran": false,
                "isFreshBlood": false,
                "isInactive": false
            }
        ]
    },
    {
        "name": "Elise's Masterminds",
        "tier": "BRONZE",
        "queue": "RANKED_TEAM_3x3",
        "entries": [
            {
                "playerOrTeamId": "TEAM-4f44680-9f2b-11e4-8954-c81f66db8bc5",
                "playerOrTeamName": "KidsClub",
                "division": "IV",
                "leaguePoints": 63,
                "wins": 6,
                "losses": 8,
                "isHotStreak": false,
                "isVeteran": false,
                "isFreshBlood": false,
                "isInactive": false
            }
        ]
    },
    {
        "name": "Elise's Horde",
        "tier": "BRONZE",
        "queue": "RANKED_TEAM_5x5",
        "entries": [
            {
                "playerOrTeamId": "TEAM-4f457640-9f2b-11e4-8954-c81f66db8bc5",
                "playerOrTeamName": "KidsClub",
                "division": "I",
                "leaguePoints": 28,
                "wins": 6,
                "losses": 4,
                "isHotStreak": false,
                "isVeteran": false,
                "isFreshBlood": false,
                "isInactive": false
            }
        ]
    }
]

}

他们问题是String是动态的。 我必须得到" 193461232"在我可以使用String之前。 我也有问题,因为名称,队列,条目是数组 我先用Simple Jsons练习,但这对我来说真的很难。

抱歉,我的英语不是很好,但我希望你仍能理解我的问题。

1 个答案:

答案 0 :(得分:1)

“193461232”可能与请求相关,换句话说我不认为它是随机的。它甚至可能是您的playerId - 值显示为第一组playerOrTeamId(相关的playerOrTeamName与您的相似)。也就是说,我不知道Id与返回的其他两个项目有什么关系。即使你不知道它,你也可以得到它。

一旦你这样做,除非你只需要json中的一两件事,你需要一些类来反序列化:

Public Class EntryItem
    Public Property playerOrTeamId As String
    Public Property playerOrTeamName As String
    Public Property division As String
    Public Property leaguePoints As Integer
    Public Property wins As Integer
    Public Property losses As Integer
    Public Property isHotStreak As Boolean
    Public Property isVeteran As Boolean
    Public Property isFreshBlood As Boolean
    Public Property isInactive As Boolean
End Class

Public Class Entry
    Public Property name As String
    Public Property tier As String
    Public Property queue As String
    Public Property entries As List(Of EntryItem)
End Class

有了这些,很简单:

Dim jstr = from whereever

Dim jobj = JObject.Parse(jstr)

Dim jName = jobj.Properties(0).Name   ' name, ie 193461232
Dim jItems = jobj(jName)              ' related items

Dim items= JsonConvert.DeserializeObject(Of Entry())(jItems.ToString)

For n As Int32 = 0 To items.Length - 1
    Console.WriteLine(items(n).name)
Next

输出是团队名称:

  易大师的烈士郎   Elise的Masterminds
  伊莉丝的部落

如果这是您的ID,则可以使用jobj("193461232")进行反序列化。当然,这可能只对你有用。其他玩家的工具应该使用jobj.Properties(0).Name,除非他们也知道他们的ID。

要访问条目数据,只需深入研究:

myWins = items(0).entries(0).wins