felipoblete
felipoblete

Reputation: 3

Deserializing JSON with square brackets VB.NET

so I have the following json string:

{
    "domain": "something.com",
    "subject": {
        "id": "1111",
        "name": "My name",
        "date": "2016-07-06"
    },
    "atributes": [
        {
            "height": "178",
            "age": "45"
        }
    ]
}

I can parse the first "round bracket" without a problem by doing this:

    Dim json = Await client.GetStringAsync(url) //gettin the json from an API
    Dim jss = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Object)(json) 

    For Each Jproperty In jss("subject")

        Dim name1 = Jproperty.Name.ToString
        Dim value1 = Jproperty.Value.ToString

        TextBox1.Text &= name1 & ": " & value 1
        //Which returns: 
        //id: 111
        //name: My name
        //date: 2016-07-06

    Next

Cool, so far so good, but I can't for the life of me doing the same with "atributes", maybe because it's inside some square brackets which make it an array I believe? When I try to do the same in ANOTHER for each I got an exception that says that "Name" cannot be found in the 'JObject' object. Using Dim height = Jproperty.Name.ToString

BUT when I call "atributes" using something like

Dim height = Jproperty.ToString (without "Name") it works but returns a giant string and that's not really what I need.

Thanks and I hope I made sense!

Upvotes: 0

Views: 638

Answers (2)

SteveCinq
SteveCinq

Reputation: 1963

If the JSON you're expecting from your API call is always of the same structure, I'd recommend creating a class and deserializing to that instead of Object. (The content in square brackets will be represented by a collection type in your class.)

Eg

Class JsonClass
    <JsonProperty("domain")>
    Property [Domain] As String
    <JsonProperty("subject")>
    Property [Subject] As cSubject
    <JsonProperty("atributes")>
    Property [Attibutes] As List(Of cAttribute)

    Class cSubject
        <JsonProperty("id")>
        Property [Id] As String
        <JsonProperty("name")>
        Property [Name] As String
        <JsonProperty("date")>
        Property [Date] As String
    End Class

    Class cAttribute
        <JsonProperty("height")>
        Property [Height] As String
        <JsonProperty("age")>
        Property [Age] As String
    End Class
End Class

Dim jss As JsonClass = Newtonsoft.Json.JsonConvert.DeserializeObject(Of JsonClass)(json) 

There is no need for any looping through raw JSON using this approach.

Upvotes: 1

Fabio
Fabio

Reputation: 32445

You are right, "square brackets" represents an Array of objects.
Notice array of objects, this mean you need another "third" foreach to loop through properties of the object.

Dim json = Await client.GetStringAsync(url)
Dim jss = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Object)(json) 

For Each property In jss("subject")
    Dim name1 = property.Name.ToString()
    Dim value1 = property.Value.ToString()

    ' ...
Next

For Each attribute In jss("attributes")
    For Each property In attribute
        Dim name1 = property.Name.ToString()
        Dim value1 = property.Value.ToString()

        ' ...
    Next
Next

Upvotes: 0

Related Questions