Bob
Bob

Reputation: 115

VB.Net multidimensional Json String

I'm not a VB.Net programmer, but have been thrust into having to do it. I've been poking at this for a few days and have searched on Google repeatedly. The more experienced programmers told me to come here and ask because they didn't know VB.net either.

I have a json string, stripped of some inner data for brevity...

{
    "_links": {
        "self": {
            "href": "URL"
        },
        "first": {
            "href": "URL"
        },
        "last": {
            "href": "URL"
        }
    },
    "_embedded": {
        "account": [
            {
                "user": "Bob",
                "text": "Some Text",
                "status": "*ENABLED",
                "_links": {
                    "self": {
                        "href": "Some URL"
                    }
                }
            },
            {
                "user": "Joe",
                "text": "Some Other Text",
                "status": "*ENABLED",
                "_links": {
                    "self": {
                        "Some Other URL"
                    }
                }
            }
        ]
    },
    "page_count": 1,
    "page_size": 25000,
    "total_items": 1109,
    "page": 1
}

I'm using .net 3.5 and can iterate through the internal items to get the data with:

Dim myData As Dictionary(Of String, Object)
myData = serializer.DeserializeObject(myResult)

Dim newData = myData.Item("_embedded")
Dim accountData= newData.Item("account")

For Each account As Object In accountData
    Dim tmpData As Dictionary(Of String, Object) = CType(account, Dictionary(Of String, Object))
    Console.WriteLine("User: " & tmpData.Item("user").ToString())
    Console.WriteLine("Account Text: " & tmpData.Item("text").ToString())
    Console.WriteLine("Status: " & tmpData.Item("status").ToString())
    tmpData.Clear()
Next

However, I have to turn "Strict On", and when I do, I get errors with the code as expected. Most of the suggestions I've read say to use copious amounts of CType, TryCast, and so on. I also hear that json.net would work better, but I have been trying for the past 3 days of Google and trying different things...

I ran the debugger with Strict off and watched what the various lines were cast as and tried to recreate to no avail...

Open to trying suggestions other than turning Strict off as I don't have that luxury...

Upvotes: 2

Views: 249

Answers (1)

Using NewtonSoft, you can iterate the data pretty easily:

Dim jstr = ...
Dim jobj = JObject.Parse(jstr)

Dim jdata = jobj("_embedded")("account")

For Each j As JObject In jdata
    Console.WriteLine("User: " & j.Item("user").ToString())
    Console.WriteLine("Account Text: " & j.Item("text").ToString())
    Console.WriteLine("Status: " & j.Item("status").ToString())
Next

Output:

User: Bob
Account Text: Some Text
Status: *ENABLED
User: Joe
Account Text: Some Other Text
Status: *ENABLED

Rather than just printing, you could store to a collection. You could also use some classes to create a typed collection directly:

Public Class Account
    Public Property user As String
    Public Property text As String
    Public Property status As String
    <JsonProperty("_links")>
    Public Property Links As LinkInfo
End Class

Public Class Self
    Public Property href As String
End Class

Public Class LinkInfo
    Public Property self As Self
End Class

That is the structure of the "account" portion of the json, so now we can deserialize to a List(Of Account):

Dim jobj = JObject.Parse(jstr)
Dim jdata = jobj("_embedded")("account")
Dim myAccts = JsonConvert.DeserializeObject(Of List(Of Account))(jdata.ToString)

myAccts will be a List containing an Account item for each thing in the json. Note that some of those classes may need work - the limited sample of 2 may not give a complete picture of what they look like.

Upvotes: 2

Related Questions