Parse Simple JSON with VB.NET

5.3k views Asked by At

I have a simple JSON string that I am trying to parse with Visual Studio Express 2010 using native .net 4.0 instead of NewtonSoft. The json data that I am trying to parse looks like the following.

"{"token_type":"Bearer",""expires_in":3599,"access_token":"VxwK6YWYj6paqyMK2D2r4uDl34qg"}"

I can get the following code to run without error but when I try to dump the contents of the object I don't have any in the list. Here is the class I created.

Public Class AuthToken

  Public Property token_type As String
      Get
          Return m_token_type
      End Get
      Set(ByVal value As String)
          m_token_type = value
      End Set
  End Property
  Private m_token_type As String
  Public Property expires_in() As Integer
      Get
          Return m_expires_in
      End Get
      Set(ByVal value As Integer)
          m_expires_in = value
      End Set
  End Property
Private m_expires_in As String
Public Property access_token As String
      Get
          Return m_access_token
      End Get
      Set(ByVal value As String)
          m_access_token = value
      End Set
 End Property
 Private m_access_token As String
End Class

My feeling is that my problem is in my class but I am not sure. So after looking for hours on this site and others Trouble parsing Json into .net Object I have put together the following code to parse the information and dump it to a RichTextBox just to see what it is.

Dim sr As New StreamReader(req.GetResponse.GetResponseStream)
Dim authtoken As New List(Of AuthToken)()
Dim ms As New MemoryStream(System.Text.Encoding.Unicode.GetBytes(sr.ReadToEnd))
Dim serializer As New System.Runtime.Serialization.Json.DataContractJsonSerializer(authtoken.GetType)
authtoken = DirectCast(serializer.ReadObject(ms), List(Of AuthToken))
ms.Close()
ms.Dispose()
sr.Close()
sr.Dispose()
For Each token In authtoken
        rtbResponse.AppendText("Token: " & token.access_token & " Expires in: " & token.expires_in)
Next

So is my class created incorrectly? Is the data from the memorystream just not getting into the authtoken object because the class doesn't match the contents of the json data as it is deserialized?

If I am using the "DataContractSerializer" do I need to have data contract "stuff" in my class?

Any help is GREATLY appreciated.

1

There are 1 answers

0
Maximilian Riegler On BEST ANSWER

The data is not getting into the authtoken variable because you don't have a list of auth tokens there - only one object. So if you get those JSON objects one at the time get rid of your lists and do it like that:

Dim sr As New StreamReader(req.GetResponse.GetResponseStream)
Dim authtoken As AuthToken
Dim ms As New MemoryStream(System.Text.Encoding.Unicode.GetBytes(sr.ReadToEnd))
Dim serializer As New System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(AuthToken))

authtoken = DirectCast(serializer.ReadObject(ms), AuthToken)

ms.Close()
ms.Dispose()
sr.Close()
sr.Dispose()

rtbResponse.AppendText("Token: " & authtoken.access_token & " Expires in: " & authtoken.expires_in)