wambo
wambo

Reputation: 77

How to parse a dynamic Json String using the Json Api

Im currently trying to parse this String into my program with the API from 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
            }
        ]
    }
]

}

They Problem is the String is dynamic. I have to get "193461232" first before i can use the String. I have also the problem because name, queue, entries are arrays And I practiced with Simple Jsons first but this one is really hard for me.

Sorry my english is not really good but I hope you can still understand my Problem.

Upvotes: 1

Views: 140

Answers (1)

"193461232" is probably something related to the request, in other words I dont think it is random. It may even be your playerId - the value also appears as the playerOrTeamId for the first group (and the related playerOrTeamName is similar to yours here). That said, I do not know how that Id relates to the other two items returned. Even if you don't know it, you can get it.

Once you do, unless you only need one or two things from the json, you need some classes to deserialize to:

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

With those, its pretty simple:

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

The output is the team names:

Master Yi's Pyromancers
Elise's Masterminds
Elise's Horde

If that is your Id, you can just use jobj("193461232") to deserialize. Of course that may only work for you. A tool for other players should use jobj.Properties(0).Name unless they know their Id too.


To access the entry data, just drill into it:

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

Upvotes: 1

Related Questions