Dynamically getting data from multidimensional JArrays

Posted: Friday 8 March 2013

JSON.net is an incredibly horrible library to parse through arrays. JArray.parse is one way JSON data can be parsed, but it cannot be accessed in a manner which other programming languages, like javascript can.
Here's JSON code from a project I'm currently working on:

{"game": {"turn": 0, "state": [["s9", "h0", "c10", "d11", "s4", "0"], ["s7", "s1", "d2", "c3", "s10", "0"]]}}
It's valid JSON, you can check it for yourself here: http://jsonlint.com/
The hierarchy is like this:

  • Game
    • Turn
    • State
      • Array
        • Array
Grabbing the data from Game and Turn is basic, handled using JObject:
JObject.Parse(JObject.Parse(tempts).SelectToken("game").ToString()).SelectToken("turn")
However, the luxury of using SelectToken is not available with JArray
Any attempt to access it as an array will fail, that's where this error message comes from: Additional information: Accessed JArray values with invalid key value: "0". Array position index expected
 So you can access it like this:

        Dim game2 = JObject.Parse(JObject.Parse(tempts).SelectToken("game").ToString()).SelectToken("state")
        MsgBox(game2(0)(0))
And hopefully get "s9" as your output..
When it comes to dynamic variables in a loop however:
        For i = 0 To Val(1)
            Dim game2 = JObject.Parse(JObject.Parse(tempts).SelectToken("game").ToString()).SelectToken("state")
            MsgBox(game2(i)(0))
        Next

Additional information: Accessed JArray values with invalid key value: 0. Array position index expected.
This is because JArray does not accept the loop's value as "Val"
        For i = 0 To 1
            Dim game2 = JObject.Parse(JObject.Parse(tempts).SelectToken("game").ToString()).SelectToken("state")
            MsgBox(game2(i)(0))
        Next

Will work as intended, where Val(1) is replaced with "1" or even Int(1)
However if you need to use Val, then here's a hacky solution for that..

        Dim pIndexed = 0
        For Each token In game
            If pIndexed = p Then
                Dim iIndexed = 0
                For Each token2 In token
                    If iIndexed = i Then
                        currentValu = token2.ToString
                    Else
                    End If
                    iIndexed = iIndexed + 1
                Next

            Else
            End If
            pIndexed = pIndexed + 1
        Next