Get items in response object via javaScriptSerializer.Deserialize

288 views Asked by At

I have a dropbox webhook hitting my page. I need to grab the list of users in the response which I will process later on a separate thread. I can see the users in the data variable but I don't know how to extract the list of users in the object. Basically I want to populate an array of users which I can loop through and do some other processing. Hope this makes sense.

This is what the object looks like:

{
"delta": {
    "users": [
        12345678,
        23456789,
        ...
    ]
}

}

This is the code I tried and like I say, I can see the string in data:

Dim strJSON = [String].Empty
            Context.Request.InputStream.Position = 0
            Using inputStream = New StreamReader(Context.Request.InputStream)
                strJSON = inputStream.ReadToEnd()
            End Using
            Dim javaScriptSerializer As New JavaScriptSerializer()
            Dim data As Object = javaScriptSerializer.Deserialize(strJSON, GetType(Object))

I would like an array of the users. Hope you can help.

1

There are 1 answers

0
Brian Rogers On BEST ANSWER

Just make some classes for your response data:

Class Data
    Public Property delta As Delta
End Class

Class Delta
    ' If you would rather have a list you can declare this As List(Of Integer) instead
    Public Property users As Integer()
End Class

You can then deserialize directly into the classes:

Dim data As Data = javaScriptSerializer.Deserialize(Of Data)(strJSON)

From there you can work with your data easily enough:

For Each user As Integer In data.delta.users
    Console.WriteLine(user)
Next